c++ - 有没有办法使用对象及其非空参数列表调用运算符来生成 std​​::thread ?

标签 c++ multithreading c++11 stdthread function-call-operator

总的来说,我对 std::thread 和 C++11 不太熟悉。尝试使用 https://en.cppreference.com/w/cpp/thread/thread/thread 中的示例,我试图看看是否可以使用带有非空参数列表的类成员函数调用运算符来生成 std::thread ,如下面的代码所示:

// main.cpp

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

class Foo {
public:

  void operator()( int& i ) {
    std::cout << i << std::endl;
  }
};

int main( int argc, char* argv[] ) {
  Foo f;
  int i = 42;

  std::thread t1( f, i );
  t1.join();

  return 0;
}

错误消息很神秘:

$ g++ --version && g++ ./main.cpp -lpthread && ./a.out
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

In file included from /usr/include/c++/6/thread:39:0,
                 from ./main.cpp:5:
/usr/include/c++/6/functional: In instantiation of ‘struct std::_Bind_simple<Foo(int)>’:
/usr/include/c++/6/thread:138:26:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = Foo&; _Args = {int&}]’
./main.cpp:19:24:   required from here
/usr/include/c++/6/functional:1365:61: error: no type named ‘type’ in ‘class std::result_of<Foo(int)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^~~~~~~~~~~
/usr/include/c++/6/functional:1386:9: error: no type named ‘type’ in ‘class std::result_of<Foo(int)>’
         _M_invoke(_Index_tuple<_Indices...>)

相比之下,空参数列表调用运算符工作正常:

// main.cpp

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

class Foo {
public:

  void operator()() {
    std::cout << 42 << std::endl;
  }
};

int main( int argc, char* argv[] ) {
  Foo f;
  int i = 42;

  std::thread t1( f );
  t1.join();

  return 0;
}
$ g++ --version && g++ ./main.cpp -lpthread && ./a.out
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

42

我的第一次尝试是否可行 - 我只是有语法错误吗?有没有办法使用对象及其非空参数列表调用运算符来生成 std​​::thread ?


我相信这个问题与 Start thread with member function 不同因为这个问题具体是关于使用成员对象调用运算符生成线程,我知道这可以使用 lambda 来完成。

最佳答案

std::thread 不允许您通过引用传递,除非您是明确的,因为它很容易导致生命周期问题。使用 std::ref 明确表明您正在通过引用传递 i:

std::thread t1( f, std::ref(i) );

或者,按值传递。在通过引用将某些内容传递给线程之前请仔细考虑并确保它是必要的。您传递的变量必须比它在线程内使用的生命周期长。

关于c++ - 有没有办法使用对象及其非空参数列表调用运算符来生成 std​​::thread ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64002799/

相关文章:

c++ - C++ 中是否有一种有效的方法可以在运行时检索函数的*完整签名*?

Java同步问题

java - 有没有办法取消并重用ExecutorService?

python - 将 C++ 数组发送到 Python 并返回(使用 Numpy 扩展 C++)

c++ - 为什么允许嵌套类模板的部分特化,而不允许完全特化?

c++ - 如何概括作用于不同类型 vector 的函数?

c++ - 先前初始化内存是否保证在放置新调用后持续存在?

c++ - 在 C++ 中从输入函数获取参数类型的模板函数出错?

c++ - "incorrect behaviour"++ 和 += 运算符不修改原始对象吗?

linux - 在多线程程序中为 Inotify 添加 watch