از std::condition_variable استفاده کنید:
#include <iostream>
#include <vector>
#include <thread>
#include <algorithm>
#include <chrono>
using namespace std;
struct Foo{
Foo():write_flag(false){}
std::mutex write_mtx;
std::std::vector<<int> items ;
std::condition_variable cv;
std::atomic_bool write_flag;
void writeOp(int value){
write_flag = true;
std::lock_gaurd<std::mutex> lck(write_mtx);
items_.push_back(value);
write_flag = false;
cv.notify();
}
int searchOp(int value){
{
std::unique_lock<std::mutex> lk(m);
c.wait(lk,[&]{return !write_flag;});
lk.unlock();
}
int result =-1;
auto res = std::find(items.begin(),items.end(),value);
if (res != items.end())
result = std::distance(items.begin(),res);
return result;
}
};