Compiling Multithreaded Code with g : 'std::system_error' Resolved
Encountering the error 'std::system_error' while compiling multithreaded code with g can be frustrating. Despite explicitly specifying the '-pthread' flag, the issue persists.
Investigating the provided code, there doesn't seem to be any apparent problem. However, the issue arises from a bug in gcc. The workaround provided by a member of the SO C chat offers a solution:
-Wl,--no-as-needed
When added to the compilation command, this flag resolves the issue, allowing the multithreaded code to compile and run successfully. The updated compilation command would be:
$ g++ main.cpp -o main.out -pthread -std=c++11 -Wl,--no-as-needed
The above is the detailed content of Why Does My Multithreaded Code Still Throw \'std::system_error\' Even With \'-pthread\'?. For more information, please follow other related articles on the PHP Chinese website!