简体   繁体   中英

c++: PThread Scheduling vs. Windows Thread

I have some experience with windows threads and am looking into pthreads. I've a question about the pthread scheduling. On windows you can set the Process-Priority and within a Process you can set the process-Priority. The Windows Scheduler executes always the process with the highest Process-Priority, and in the Process the Threads on a Round-Robin.

Now to the pthreads you can set the scheduler-policy via sched_setscheduler, and set priority for real-time-modi. Everything, wright so far?

QUESTIONS:

1) How are pthreads scheduled over different processes?

2) Are real-time-threads performed before threads with lower priority or is there a round-robin?

3) How can i get as much realtime-behavior (fe for i/o -signals) as possible and how much can i get?

It is important to realise that POSIX threads (pthreads) are specified in a standard that imposes certain minimum requirements on implementations, but also allows them considerable leeway.

pthreads has a concept of Thread Scheduling Contention Scope . pthreads implementations must support at least one of PTHREAD_SCOPE_SYSTEM and PTHREAD_SCOPE_PROCESS scheduling contention scopes.

If a thread is scheduled under the PTHREAD_SCOPE_SYSTEM contention scope, it competes equally to be scheduled with all other processes and threads using PTHREAD_SCOPE_SYSTEM on the system.

If a thread is scheduled under the PTHREAD_SCOPE_PROCESS contention scope, it only directly competes with other threads also using PTHREAD_SCOPE_PROCESS in the same process. How that group of threads is then scheduled with respect to other processes / threads is unspecified by POSIX, but typically each group of threads using PTHREAD_SCOPE_PROCESS in the same process is treated as a single PTHREAD_SCOPE_SYSTEM entity.

It is common for implementations to only support one of these - for example, Linux supports only the PTHREAD_SCOPE_SYSTEM scheduling contention scope.

Under pthreads, runnable higher priority threads always execute in preference to lower priority threads. With threads at the same priority level, if a thread is using the SCHED_FIFO scheduling policy then it will continue to run until it blocks or yields; if it is using the SCHED_RR scheduling policy then it will also be pre-empted after using up its time quantum.

I do not believe POSIX imposes any constraints on realtime performance - after all, realtime latency is strongly influenced by the underlying hardware. Apart from setting a realtime scheduling policy ( SCHED_FIFO or SCHED_RR ), you can also get more deterministic latency by locking your process memory using mlockall(MCL_CURRENT | MCL_FUTURE); and pre-faulting in the memory you require.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM