我刚开始使用vscode,我做了一个有关c ++中多线程的教程。下面的代码与教程相同,并且我确定代码可以在Visual Studio 2017中使用。
#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <fstream>
using namespace std;
std::mutex mu;
class LogFile{
std::mutex mu;
ofstream f;
public:
LogFile(){
f.open("log.txt");
}
void p(string msg, int i){
std::lock_guard<mutex> locker(mu);
f << msg << i << endl;
}
};
void function_1(LogFile& log){
for(int i=0;i>-100;i--)
log.p("from t1: ", i);
}
int main(){
LogFile log;
std::thread t1(function_1, std::ref(log));
for (int i=0;i<100;i++)
log.p("from main: ", i);
t1.join();
return 0;
}
错误是:
test.cpp:27:17: error: no matching constructor for initialization of 'std::thread'
std::thread t1(function_1, std::ref(log));
^ ~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:408:9: note: candidate constructor template not viable: requires single argument '__f', but 2 arguments were provided
thread::thread(_Fp __f)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:289:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided
thread(const thread&);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:296:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
thread() _NOEXCEPT : __t_(_LIBCPP_NULL_THREAD) {}
^
1 error generated.
有人帮助我!谢谢!
答案 0 :(得分:0)
您的代码正确,因此这似乎与xcode有关。
-std=c++11
)