#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
using namespace std;
int num = 1;
#define NUM 20
condition_variable odd;
condition_variable even;
mutex mut;
void thread_odd()
{
while(num < NUM -1)
{
if(num%2 != 1)
{
unique_lock<mutex> lock(mut);
odd.wait(lock);
}
cout<<"ODD : "<<num<<endl;
num++;
even.notify_all(); // Line X
}
}
void thread_even()
{
while(num < NUM)
{
if(num%2 != 0)
{
unique_lock<mutex> lock(mut);
even.wait(lock);
}
cout<<"EVEN : "<<num<<endl;
num++;
odd.notify_all();
}
}
int main()
{
thread t1(thread_odd), t2(thread_even);
t1.join();
t2.join();
return 0;
}
/* 위의 내용은 ODD & EVEN 번호를 동기화 방식으로 (1 개씩) 인쇄하는 프로그램입니다. 코드는 대부분 잘 작동합니다. 하지만 때때로 교착 상황에 처해 있습니다. 이상한 스레드가 notify_all을 치고 있지만 짝수 스레드가 깨어나 기 전에 홀수 스레드가 잠금을 획득 한 후 대기 조건을 발견하면 짝수 스레드가 깨어나지 않은 동안 대기 상태가됩니다. 거래 상황에서 나가기. notify_all을 notify_one, 으로 바꾸려고 시도했지만 문제가 지속됩니다. 디자인에 필요한 변경 사항이 있습니까? 또는 내가 완전히 빠진 것이 있습니까? */notifyall이 C++ 멀티 스레딩에서 작동하지 않습니다. 교착 상태 발생
'thread_odd()'가'even.notify_all()'을 호출하기 전에'thread_even()'이'even.wait()'을 호출하는지 확인해야합니다. – mkcms