程序中想创建个可传参的子线程:
void func(int a)
{
do sth...
}
int main()
{
int a = 5;
std::thread thread(func, a);
thread.detach();
}
但是编译时 xcode 提示:
No matching constructor for initialization of 'std::thread'
具体来看是 thread 构造函数只支持一个参数,而程序中传了两个:
Candidate constructor not viable: requires 1 argument, but 2 were provided
这是因为 mac 下的 g++ 使用的是 clang,默认不支持 c++11多线程。
lucas@lucasdeMacBook-Pro premiereproplugin % g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
可在 xcode 中做如下修改,即添加编译指令:-std=c++11
如果是 vscode 呢?可在配置文件 tasks.json 中 args 下同样加上:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ 生成活动文件",
"command": "/usr/bin/clang++",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-std=c++11",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
...
}
],
"version": "2.0.0"
}
以上,欢迎交流~