如何设置优先级在C / c ++中获取互斥体 [英] how to set the priority to get the mutex in C/c++
问题描述
我有3个进程(同等优先级)
- P1
-
- P3(计时器)
获得互斥体的优先级如下: P1 (1优先),P2(2优先),P3(计时器)(3优先)
如果假设
p3
来得到互斥体然后
p2
来等待互斥体之后,p1来,它也等待互斥
如果 p3
释放互斥,则 p1
互斥体 p2
。
如何在C或C ++中执行此操作。
注意:所有进程都在同一优先级的线程中运行。
OS - windows Xp
由于线程具有相等的优先级,哪个线程获得锁定将是相当任意的。看来你想等待条件变量而不是使用简单的互斥。你仍然会有互斥体;条件变量是互斥体顶部的概念。另一种可能性是使用同步屏障。
编辑:
使用pthreads接口(C风格)使用条件变量的示例:
< a href =https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal =nofollow> https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal
一个重要的问题,你需要问自己:随着所有这些等待和同步,你买什么吗?使用线程的目的是让一些东西并行运行。如果没有发生,你有一个多线程的应用程序运行速度比如果应用程序根本没有使用线程。
I have 3 process (equal priority)
- P1
- P2
- P3(timer)
priority to get the mutex is as follows: P1(1 priority), P2(2 priority), P3(timer)(3 priority)
If suppose p3
comes and get the mutex
then p2
comes and wait for mutex
after that p1 comes and it also wait for mutex
if p3
release mutex then p1
should get the mutex not p2
.
How to perform this in C or C++.
Note : all processes are running inside threads having same priority.
OS - windows Xp
Since the threads have equal priority, which thread gets the lock will be rather arbitrary. It appears you want to wait on a condition variable rather than using a simple mutex. You will still have a mutex; condition variables are a concept on top of mutexes. Another possibility is to use synchronization barriers.
EDIT: An example of using condition variables using the pthreads interface (C-style): https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal
An important question you need to ask yourself: With all of this waiting and synchronization, are you buying anything? The purpose of using threads is to let some things run in parallel. If that is not happening, you have a multithreaded application that runs slower than if the application didn't use threads at all.
这篇关于如何设置优先级在C / c ++中获取互斥体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!