ubuntu16.04 c++11 std::mutex invalid

已完成

suppose there're two threads execute the function `f` at the same time, like this:

int g_i = 0;
std::mutex g_mtx;

void f()
{
for (int i = 0; i < 10000000; ++i) {
std::lock_guard<std::mutex> lck(g_mtx);
g_i++;
}
}

the final result is not 20000000 instead of a random integer less than 20000000, even though I has append the options :

target_link_libraries(test pthread)

in the CMakeLists.txt.Obviously the mutex is invalid.

Buf if i compile the test cpp file by command line with -pthread options, the result is correct.

So how to fix this problem?

0
Avatar
Permanently deleted user

anybody can answer this question?

0
Avatar
Permanently deleted user

use option `add_compile_options(-std=c++11)` instead of `set(CMAKE_CXX_STANDARD 11)`.The result is correct, problem solved

0

请先登录再写评论。