c++ - 为什么我不能在我的项目中使用多线程?
问题描述
我想从多线程开始,但我的 clion IDE (v. 2019.3.6) 无法识别创建的线程。我也尝试使用 boost 库中的线程,但也存在常见错误(“未定义对 boost::threads 的引用”)。我在我的 clion 和 mingw 编译器中使用 c++14。
我使用 g++ 编译了 boost 库。
我尝试运行的代码:
#include <iostream> // std::cout
#include <boost/thread.hpp>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
boost::thread t1(bar, 1);
t1.join();
return 0;
}
或者:
#include <iostream> // std::cout
#include <thread>
void foo(){
// do stuff...
}
void bar(int x){
// do stuff...
}
int main(){
std::thread t1(bar, 1);
t1.join();
return 0;
}
它产生的错误:
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::start_thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:182: undefined reference to `boost::thread::start_thread_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::~thread()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:257: undefined reference to `boost::thread::detach()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::thread::join()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::thread::get_id() const'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:759: undefined reference to `boost::this_thread::get_id()'
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:762: undefined reference to `boost::thread::join_noexcept()'
CMakeFiles\client.dir/objects.a(client.cpp.obj): In function `boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > > >::~thread_data()':
C:/boost_builded_from_sources/include/boost/thread/detail/thread.hpp:94: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
CMakeFiles\client.dir/objects.a(client.cpp.obj):client.cpp:(.rdata$.refptr._ZTVN5boost6detail16thread_data_baseE[.refptr._ZTVN5boost6detail16thread_data_baseE]+0x0): undefined reference to `vtable for boost::detail::thread_data_base'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:86: client.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
或来自 std::thread 的错误线程
C:\Users\Admin\Desktop\client\client.cpp: In function 'int main()':
C:\Users\Admin\Desktop\client\client.cpp:15:10: error: 'thread' is not a member of 'std'
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:15:10: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
C:\Users\Admin\Desktop\client\client.cpp:5:1:
+#include <thread>
C:\Users\Admin\Desktop\client\client.cpp:15:10:
std::thread t1(bar, 1);
^~~~~~
C:\Users\Admin\Desktop\client\client.cpp:16:5: error: 't1' was not declared in this scope
t1.join();
^~
C:\Users\Admin\Desktop\client\client.cpp:16:5: note: suggested alternative: 'tm'
t1.join();
^~
tm
mingw32-make.exe[3]: *** [CMakeFiles\client.dir\build.make:63: CMakeFiles/client.dir/client.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/client.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/client.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: client] Error 2
为什么线程不是 std::thread 的成员?我附上了正确的标题
这可能是mingw不支持多线程吗?
也许有人遇到过这样的问题并且能够提供帮助。
解决方案
mingw 无法识别线程库。我将编译器的版本更改为另一个版本,一切正常。
推荐阅读
- java - NetBeans 平台:如何禁用特定选项对话框类别?
- javascript - Nodejs环回TCP延迟1-2ms?
- github - Github:如何从发布分支自动合并到开发分支?
- javascript - 如何在 react-bootstrap 中实现溢出隐藏?
- c++ - 无法删除在 Qt6 中自动检测到的重复的 Android 6.0.0 Clang 编译器套件
- asp.net-core - Blazor Web 程序集 - 各种 OIDC 身份验证路径之间的区别
- android - 如何在Android中处理按钮
- matlab - 通过 SVD 对卵巢癌数据进行 PCA
- node.js - 如何确保所有异步方法成功执行
- go - 将类型存储在结构中以进行比较