Program to invoke threads on the priority basis
I am trying to run threads as priority 1 and other priority threads(except 1),
In here thread t2 is of priority 2 the 3 threads t1,t2, and t3 are in wait . and thread t4 goes and calls signals with priority 2. //- I want it to invoke only threads with priority other than 1
I am trying to run threads as priority 1 and other priority threads(except 1),
In here thread t2 is of priority 2 the 3 threads t1,t2, and t3 are in wait . and thread t4 goes and calls signals with priority 2. //- I want it to invoke only threads with priority other than 1
The output of this program is
single notifyall cv.wait(master); cv1.wait(master); cv.wait(master);```
Looking for the explanation of the output
#include <condition_variable>
#include <thread>
#include <chrono>
using namespace std;
std::condition_variable cv;
std::condition_variable cv1;
std::mutex cv_m;
void waits(int priority)
{
std::unique_lock<std::mutex> master(cv_m);
if(priority == 1){
std::cout<<"cv.wait(master);"<<std::endl;
cv.wait(master);
}
else {
std::cout<<"cv1.wait(master);"<<std::endl;
cv1.wait(master);
}
std::cout<<"Priority"<<priority<<std::endl;
}
void signals(int priority)
{
switch (priority){
case 1:
std::cout<<"both notifyall"<<std::endl;
cv.notify_all();
cv1.notify_all();
break;
case 2:
std::cout<<"single notifyall"<<std::endl;
cv1.notify_all();
break;
}
}
int main()
{
std::thread t1(waits,1), t2(waits,2), t3(waits,1),
t4(signals,1);
t1.join();
t2.join();
t3.join();
t4.join();
}
I am trying to run threads as priority 1 and other priority threads(except 1),
In here thread t2 is of priority 2 the 3 threads t1,t2, and t3 are in wait . and thread t4 goes and calls signals with priority 2. //- **I want it to invoke only threads with priority other than 1**
**The output of this program is**
single notifyall
cv.wait(master);
cv1.wait(master);
cv.wait(master);```
but the priority 1 threads were never signaled.
**Probable explanation from my side:**
The reason is the condition on which condition variables are waiting.
Here they are waiting on Locks, with no other condition and once the Thread 4 - Signals, it says that the master lock is available, and it becomes available for both priority 1 and other priority threads, so any thread waiting on the lock picks it.
Looking for better ideas....................... and Reasons if Any
Aucun commentaire:
Enregistrer un commentaire