I have 3 process (equal priority)
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.
SetThreadPriority(
HANDLE hThread,
int nPriority
);
this function will set the priority of your threads .... the HANDLE
value you will get while creating thread.. like
:HANDLE hf=_beginthred(abc,0,NULL)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With