C++简单的线程启动问题
C++ Simple Thread Start Issue
我有相当简单的代码,我找到的每个示例看起来都非常相似。 我确定我缺少一些基本的东西,所以任何帮助将不胜感激。
/*-------------------------------------
State
--------------------------------------*/
void Receiver::start(int num_threads = 1) {
_kill_mtx.lock();
_kill_state = false;
_kill_mtx.unlock();
for (int i=0; i<num_threads; ++i)
threads.push_back(std::thread(thread_func)); // LINE 24
std::cout << threads.size() << " now running.n";
std::cout << std::thread::hardware_concurrency() << " concurrent threads are supported.n";
}
void Receiver::stop() {
}
/*-------------------------------------
Thread
--------------------------------------*/
void Receiver::thread_func() {
while(true) {
if (_kill_mtx.try_lock()) {
if (_kill_state) {
break;
}
_kill_mtx.unlock();
}
std::cout << "Still Alive!" << std::endl;
}
}
它使用 -std=c++0x -lstdc++ -ldbus-c++-1 -pthread
编译并输出错误:
communication.cpp: In member function 'void Receiver::start(int)':
| communication.cpp:24:47: error: no matching function for call to 'std::thread::thread(<unresolved
overloaded function type>, int&)'
| communication.cpp:24:47: note: candidates are:
| /home/toor/bluegiga-sdk/build/tmp/sysroots/apx4devkit/usr/include/c++/thread:133:7: note: std::th
read::thread(_Callable&&, _Args&& ...) [with _Callable = void (Receiver::*)(int), _Args = {int&}]
| /home/toor/bluegiga-sdk/build/tmp/sysroots/apx4devkit/usr/include/c++/thread:133:7: note: no kn
own conversion for argument 1 from '<unresolved overloaded function type>' to 'void (Receiver::*&&)
(int)'
GCC 编译器是由我正在构建的 SDK 提供的 ARM 编译器。 到目前为止,它支持我尝试过的大多数其他 C++11 功能,所以我认为这不是问题所在,但我不确定。
我猜Receiver::thread_func
是一个非静态成员函数,在这种情况下,您需要传递隐式的第一个参数。假设您希望线程在同一实例中运行,则需要传递this
。否则,您需要传递指向另一个Receiver
实例的指针:
threads.push_back(std::thread(&Receiver::thread_func, this));
相关文章:
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- 在我的以下代码中获取 MLE(内存限制错误).尝试解决 ROUND C 2019(问题 A-摆动行走)启动问题
- 谷歌启动B轮巴士路线问题错误的答案?
- Win7 C++ - 从以用户身份登录的服务启动可执行文件的问题
- 解决谷歌启动2018年第三季度的启动问题
- 从另一个应用启动我的应用时出现相对路径更改问题
- 生成文件问题(体系结构x86_64的未定义符号:"_main",引用自:主可执行文件的隐式入口/启动)
- 有关启动引用指针的引用的问题
- 在Qt和C++中从另一个应用程序启动一个应用软件会产生问题
- 我有一个 c++ 代码,它在启动时崩溃,然后到达问题所在 main() 的执行
- C++简单的线程启动问题
- 从QtCreator启动OpenGl应用程序的严重问题
- Qt QState机器同步问题:初始状态未在启动信号上设置
- 问题:如何在启动新流程后关闭应用程序
- 调试混合代码应用程序中的启动问题
- 银行账户程序的构建和启动问题
- 从静态初始化代码启动CUDA内核时遇到问题
- 在c++中启动、停止和重新启动线程时的问题
- [C++]:关于构造函数中的启动列表的问题
- 一些系统默认. link文件从假冒用户下启动的问题