ubuntu 16.04、gcc 5.4.0、cmake 3.5.1
有什么区别,哪个更好?
承诺.cpp
std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10); // throw std::exception and terminate
Run Code Online (Sandbox Code Playgroud)
CMakeLists.txt
add_executable(promise promise.cpp)
target_link_libraries(promise pthread)
Run Code Online (Sandbox Code Playgroud)
稍微修改 CMakeLists.txt。
add_executable(promise promise.cpp)
target_link_libraries(promise -pthread)
Run Code Online (Sandbox Code Playgroud)
我从这里找到了答案。但我不知道为什么?
但是,最好的解决方案是便携。
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
target_link_libraries(promise Threads::Threads)
Run Code Online (Sandbox Code Playgroud)