I would like to know what user-space threading libraries (for standard C++) are available which allow for fine-grained control over application level preemptive scheduling. Target platform is POSIX but having a cross platform solution would be particularly nice.
-
1What does "fine-grained control over application level preemptive scheduling" mean? What kind of control, how/when? Assuming standard `pthreads` isn't good enough, what do you need? – Mats Petersson Mar 10 '13 at 20:02
-
I would like to implement application level scheduling - so I would like to have the ability to make threads (which may be waiting over a mutex) sleep, and to wake them up later (and restore their state) and to set the priorities. – lorefnon Mar 10 '13 at 20:21
-
It wouldn't surprise me if there is such a library, but I don't know of one. – Mats Petersson Mar 10 '13 at 20:24
-
So you want "thread package" that allocate stacks per thread, but does not use the OS to do scheduling. This gets hard because you need to trap all calls that might block so you can do a thread switch until the blocking call is done. – brian beuning Mar 10 '13 at 21:14
3 Answers
If it is sufficient to set priorities of threads, you can use pthreads: How to increase thread priority in pthreads?. If you want to be able to schedule manually, then you should use a cooperative threading such as longjump or http://www.gnu.org/software/pth/pth-manual.html. To use multiple processors you will need kernel threads, which you can get through pthreads or OpenMP.
Basically you should initially spawn a couple of kernel threads, pin them, and run additional cooperative threads on top of the kernel threads. Be aware that cooperative threading is non-preemptive unless you implement the preempting with timer events, but the kernel threads will be preempted. Some cooperative threading schemes do not work well with C++, specifically they may call destructors of variables on the stack when switching.
C++11 standard library has threading support, thought it relies on pthreads on Linux. You can also use pthreads directly (it is implemented in C). If you want something portable - Qt has good thread support.

- 7,833
- 4
- 41
- 65
Portable Runtime System has customizable user space preemptive scheduling, but is written in C.

- 16