future 와 promise 는 공유버퍼로 연결되어있다. (UD. 대부분 heap 할당이 이루어진다.)
통신채널인 것이다.
future
와 promise
는 단발성 시그널 통신용으로 사용할 수 있다.
#include <iostream>
#include <future>
#include <thread>
using namespace std;
promise<void> p;
void react() { cout << "!"; };
void detect()
{
// thread 를 잔뜩 대기생성 시켜 둘 수 있다.
auto s = p.get_future().share();
vector<thread> workers;
for (int i = 0; i < 10; i++) {
workers.emplace_back(
[&s] {
s.wait(); // 차렷 열중_쉬엇 !
react();
});
}
// 뻘 짓
for (int i = 0; i < 100; i++) {
cout << "_";
}
// 스타또
p.set_value(); // 쉬엇 ! (못 쉼)
for (auto& t : workers)t.join(); // 쉼 (지옥에서)
}
int main()
{
detect();
}