2016-06-06 3 views
0

스레드 함수에 인수를 전달하려고했지만 수행 할 수 없었습니다. 나는 그것에 대해 읽으려고했지만 (Passing multiple arguments to a threaded function) 나는 아직도 그것을 알아낼 수 없었다.멀티 스레딩 C++ 함수에 인수 전달

#include <iostream> 
#include <thread> 

using namespace std; 

void func(int t) 
{ 
    cout << t << endl; 
} 

int main() 
{ 
    thread t1(func,4); 
    t1.join(); 
    return 0; 
} 

내가이 수행하여 (나는 문제가 넣다 zsh을 사용) 명령 행에서이를 실행 : 여기 내 코드입니다

g++ test.cpp 
./a.out 

을하지만 이러한 오류를 가지고 :

thread_test.cpp:14:12: error: no matching constructor for initialization of 'std::__1::thread' 
thread t1(func,4); 
    ^~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:379: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/bin/../include/c++/v1/thread:268: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/bin/../include/c++/v1/thread:275:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided 
thread() _NOEXCEPT : __t_(0) {} 
^ 
1 error generated. 

이 중요한 경우 OSX 10.11.4와 함께 Mac을 사용하고 있습니다.

또한 컴파일 할 C++의 버전을 확인하려면 이 명령을 실행하고 넣어 이것을 가지고 :

g++ --version 

로 구성 : --prefix =/응용 프로그램/Xcode.app/내용/개발자는/usr --with-Gxx라는-포함-DIR =/응용 프로그램/엑스 코드. 애플 LLVM 버전 7.3.0 (clang-703.0.31) 대상 : x86_64- 사과 darwin15.4.0 스레드 모델 : POSIX InstalledDir : /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

+2

재생할 수 없음 : http://ideone.com/BFRbzc. 어떤 컴파일러를 사용하고 있습니까? –

+1

[정상 clang과 함께 작동] (http://coliru.stacked-crooked.com/a/f95035aaa16f9fc4), 그래서 아마 애플은 다시 무언가를 망쳤습니다. 그래도 좋은 테스트 케이스. –

+2

[가능한 복제본] (http://stackoverflow.com/questions/22031640/thread-in-c-in-macos-x)'--std = C++ 11' (또는 더 높은). –

답변

1

패스 -std=c++11 그것은 작동합니다; 내 성적표를 아래에서보십시오. (OS X에서 실행 중)

nathanst% g++ t.cpp 
t.cpp:13:12: error: no matching constructor for initialization of 'std::__1::thread' 
    thread t1(func,4); 
     ^~~~~~~ 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:379: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/bin/../include/c++/v1/thread:268: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/bin/../include/c++/v1/thread:275:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided 
    thread() _NOEXCEPT : __t_(0) {} 
    ^
1 error generated. 
nathanst% g++ -std=c++11 t.cpp 
+0

고마워요! 아직도 하나의 질문이지만, -std = C++ 11은 무엇이며 왜 나는 그것을 필요로합니까? 내가 C++ 11을 사용하지 않아서 그렇다면 어떻게 C++ 11로 업데이트 할 수 있습니까? – mathew

+0

사용중인 컴파일러는 새 C++ 11 기능을 알고 있지만 기본적으로 해제되어 있습니다. -std = C++ 11은 컴파일러에게 해당 기능을 켜도록 지시합니다. 어느 시점에서 최신 버전이 기본적으로 켜지면 더 이상 해당 스위치가 필요하지 않습니다. –