我不明白,我正在进行多线程编程,它本身非常简单,它对给定的向量进行一些操作。
如果我执行我的主要是这个:
Workers workers(4);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
for(int i = 0; i < 5000; i++) {
workers.printThread(-1,"[ Asking for initialization ]");
std::vector<int> vector;
workers.initialize(vector,100);
workers.waitResult();
displayArray(vector);
workers.printThread(-1,"[ Asking for maximum ]");
workers.maximum(vector);
workers.waitResult();
std::cout << "Maximum = " << workers.getResult() << std::endl;
workers.printThread(-1,"[ Asking for number of occurences ]");
workers.nbOccurences(vector,10);
workers.waitResult();
std::cout << "Number of occurence for ' " << 10 << " ' = " << workers.getResult() << std::endl;
workers.printThread(-1,"[ Asking for first occurence ]");
workers.firstOccurence(vector,10);
workers.waitResult();
std::cout << "First occurence for ' " << 10 << " ' = " << workers.getResult() << std::endl;
}
return 0;
如果我的循环迭代条件不是一个庞大的数字(只有1,5,10 ..在循环中转,没有问题,但是当我执行这几种类型时,我得到一个错误,因为我的一个线程是没有醒来..更清楚的是,这是我怀疑是问题的代码:
void Workers::threadsRoutine(int i)
{
printThread(i,"Co-Routine");
std::unique_lock<std::mutex> lk(cvMutex);
cv.wait(lk,[this,i]{printThread2(i,currentOrder != Order::NONE); return currentOrder != Order::NONE;});
printThread(i,"is wake up");
lk.unlock();
printThread(i,"Gonna execute order");
switch(currentOrder){
case Order::INITIALIZE:
printThread(i,"gonna initialize");
initializeThreaded(i,*pointer);
printThread(i,"initialize done");
break;
case Order::MAXIMUM:
printThread(i,"gonna compute maximum");
maximumThreaded(i,*pointer);
printThread(i,"maximum computed");
break;
case Order::NB_OCCURENCE:
printThread(i,"gonna compute number of occurences");
nbOccurencesThreaded(i,*pointer);
printThread(i,"number of occurences computed");
break;
case Order::FIRST_OCCURENCE:
printThread(i,"gonna compute first occurence");
firstOccurenceThreaded(i,*pointer);
printThread(i,"first occurence computed");
break;
case Order::STOP:
if(barrier->wait()){
currentOrder = Order::NONE;
endResult = true;
cvWaitResult.notify_one();
}
return;
break;
default:
std::cout << "Error" << std::endl;
break;
}
if(barrier->wait()){
currentOrder = Order::NONE;
endResult = true;
cvWaitResult.notify_one();
}
threadsRoutine(i);
}
这是一个调用notify_all
的调用方法void maximum(std::vector<int>& path)
{
results = INT_MIN;
endResult = false;
currentOrder = Order::MAXIMUM;
cv.notify_all();
}
所以基本上,我打印了很多东西,这是输出:
{ Thread N° 1 boolean value : 1 }
{ Thread N° 1 : is wake up }
{ Thread N° 1 : Gonna execute order }
{ Thread N° 1 : gonna compute number of occurences }
{ Thread N° 1 : number of occurences computed }
TMP nb threads = 1
{ Thread N° 0 : Co-Routine }
{ Thread N° 0 boolean value : 0 }
{ Thread N° -1 : [ Asking for first occurence ] }
{ Thread N° 2 : Co-Routine }
{ Thread N° 1 : Co-Routine }
{ Thread N° 2 boolean value : 0 }
{ Thread N° 3 : Co-Routine }
{ Thread N° 1 boolean value : 0 }
{ Thread N° -1 : FIRST }
{ Thread N° -1 : NOTIFY }
{ Thread N° 3 boolean value : 1 }
{ Thread N° 3 : is wake up }
{ Thread N° 3 : Gonna execute order }
{ Thread N° 3 : gonna compute first occurence }
{ Thread N° 3 : first occurence computed }
TMP nb threads = 4
{ Thread N° 0 boolean value : 1 }
{ Thread N° 0 : is wake up }
{ Thread N° 0 : Gonna execute order }
{ Thread N° 0 : gonna compute first occurence }
{ Thread N° 0 : first occurence computed }
TMP nb threads = 3
{ Thread N° 2 boolean value : 1 }
{ Thread N° 2 : is wake up }
{ Thread N° 2 : Gonna execute order }
{ Thread N° 2 : gonna compute first occurence }
{ Thread N° 2 : first occurence computed }
TMP nb threads = 2
它真正开始问第一次出现,但在上面,这是之前的操作。
正如你所看到的,在这种情况下我的线程N°1永远不会被唤醒,因为根据它没有消息......
很抱歉,如果我的解释很糟糕,请问我是否是这样,我会尝试重新格式化。
由于