簡體   English   中英

多線程c ++將參數傳遞給函數

[英]multithreading c++ passing arguments to a function

我一直在嘗試將參數傳遞給線程函數,但一直未能通過。 我嘗試閱讀有關它的內容(將多個參數傳遞給線程函數 ),但是我仍然無法弄清楚。 這是我的代碼:

#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.

如果這很重要,則我將Mac與OSX 10.11.4一起使用

另外,要查看我使用的是哪個版本的c ++,我運行了以下命令並將其輸出:

g++ --version

配置為:--prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / Applications / Xcode.app / Contents / Developer / Platforms / MacOSX.platform / Developer / SDKs / MacOSX10.11.sdk / usr / include / c ++ / 4.2.1 Apple LLVM版本7.3.0(clang-703.0.31)目標:x86_64-apple-darwin15.4.0線程模型:posix InstalledDir:/Applications/Xcode.app/目錄/開發人員/工具鏈/XcodeDefault.xctoolchain/usr/bin

通過-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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM