Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the priority to get the mutex in C/c++

I have 3 process (equal priority)

  1. P1
  2. P2
  3. 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

like image 383
Suri Avatar asked May 30 '11 09:05

Suri


2 Answers

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.

like image 170
David Hammen Avatar answered Oct 01 '22 22:10

David Hammen


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) 
like image 34
ANJi Avatar answered Oct 01 '22 20:10

ANJi