不能使用参数调用线程函数 [英] Calling threaded functions with arguments not possible

查看:115
本文介绍了不能使用参数调用线程函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题似乎与已经回答过的问题相似,但是由于给出的答案对我不起作用,因此我不认为这个问题是对他们的重复.

我很清楚这个问题:我如何在一个具有1个或多个参数的线程中调用c ++函数-在这里和在各种教程中都已多次回答-在每种情况下,答案都是只是这是这样做的方法:

I am well aware that the question: how do i call a c++ function as a thread which has 1 or more arguments has been answered several times -- both here, and on various tutorials -- and in every case the answer is simply that this is the way to do it:

(示例直接取自此问题)

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
    t1.join();
}

但是,我尝试复制粘贴此代码以及更多(或多或少相同的)方法示例,但是,每次我编译时(通过诸如g++ test.cpp -o test.app这样的术语(必须添加.app,因为我在Mac电脑上(请注意,这种编译方式实际上对我有用,并且错误根本不是我不知道如何编译c ++程序的结果)))程序,我收到此错误消息:

However i have tried copypasting both this code and many more (more or less identical) examples of how to do this, and yet, every time i compile (through termial as such g++ test.cpp -o test.app (the .app must be added because i am on a Mac (note that this way of compiling does in fact work for me, and that the error is simply not a result of me not knowing how to compile a c++ program))) such a program i get this error message:

test.cpp:16:12: error: no matching constructor for initialization of 'std::__1::thread'
    thread t1(task1, "Hello");
           ^  ~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/thread:389: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:297: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:304:5: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    thread() _NOEXCEPT : __t_(0) {}

我的问题是,与所有可能使用参数进行线程化的人相比,我做错了什么,并且由于我没有发现遇到类似问题的人所提出的任何问题,因此我不认为这是对的质疑许多如何使用参数调用线程函数

My question therefor is, what am i doing wrong compared to all the people who possitively can make threaded functions with arguments, and since i have not found any questions asked by people experiancing similar problems, I do not regard this question a duplicate of the many How do i call a threaded function with arguments

据我所知,使用线程不需要任何特定的编译器标志,并且由于我完全可以运行带有线程函数且没有参数的程序,因此您不能断言我的计算机或编译器无法完全使用线程.

As far as i know, using threads doesn't require any specific compiler flags, and since i perfectly fine can run programs with threaded functions without arguments, you can not claim that my computer or compiler is incabable of using threads altogether.

推荐答案

根据gcc版本,您应该添加编译器开关-std = c ++ 11或-std = c ++ 0x.

Depending on gcc version, you should add the compiler switch -std=c++11, or -std=c++0x.

这篇关于不能使用参数调用线程函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆