#include <iostream>
#include <thread>
#include <future>
using namespace std;
void ThreadWork(std::promise<int> promise)
{
cout << "thread begin\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
promise.set_value(999); ///< 전달 값 셋팅
cout << "thread end\n";
}
int main(int argc, char* argv[])
{
// Demonstrate using promise<void> to signal state between threads.
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread new_work_thread(ThreadWork, std::move(promise));
cout << "wait future\n";
future.wait();
cout << "future value : " << future.get() << "\n"; // 위의 "future.wait"이 없으면 future.get()에서 쓰레드가 끝날때까지 블록됨
cout << "wait thread\n";
new_work_thread.join();
cout << "end main\n";
return 0;
}
output
wait future
thread begin
thread end
future value : 999
wait thread
end main
댓글 없음:
댓글 쓰기