函数的简单生成为 std::thread

问题描述

同志们!

为了检查 gcc 将生成代码,我正在尝试做一些多线程的事情。简化的测试用例在这里

#include <iostream>
#include <thread>

int  n;
void embiggen(void) { ++n; }

void foo(void)
{
  std::thread t1(embiggen);
  t1.join();
  std::cout << "n = " << n << '\n';
};

当然,这会导致此失败:

/xtools/aarch64-unkNown-elf/bin/aarch64-unkNown-elf-g++ -ggdb3 -std=c++11 -Wall -Wextra -Wno-unused-parameter -O0 -lpthread -march=armv8.1-a foo.cc -c foo.o;
foo.cc: In function 'void foo()':
foo.cc:9:26: error: no matching function for call to 'std::thread::thread(void (&)())'
    9 |   std::thread t1(embiggen);
      |                          ^
/xtools/aarch64-unkNown-elf/aarch64-unkNown-elf/include/c++/11.1.0/bits/std_thread.h:157:5: note: candidate: 'std::thread::thread(std::thread&&)'
  157 |     thread(thread&& __t) noexcept
      |     ^~~~~~
/xtools/aarch64-unkNown-elf/aarch64-unkNown-elf/include/c++/11.1.0/bits/std_thread.h:157:21: note:   no kNown conversion for argument 1 from 'void()' to 'std::thread&&'
  157 |     thread(thread&& __t) noexcept
      |            ~~~~~~~~~^~~
/xtools/aarch64-unkNown-elf/aarch64-unkNown-elf/include/c++/11.1.0/bits/std_thread.h:121:5: note: candidate: 'std::thread::thread()'
  121 |     thread() noexcept = default;
      |     ^~~~~~
/xtools/aarch64-unkNown-elf/aarch64-unkNown-elf/include/c++/11.1.0/bits/std_thread.h:121:5: note:   candidate expects 0 arguments,1 provided

我相信我已经尝试过像 thisthis 这样的代码,但无济于事。

问:将函数生成为单独线程的咒语是什么?

编辑:我正在使用 gcc-11.1.0

EDIT2:我的编译器是由 crosstool-ngCT_THREADS="none" 创建的。我认为这是这里的杀手...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)