首页 > 解决方案 > 为什么我不能在我的项目中使用多线程?

问题描述

我想从多线程开始,但我的 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不支持多线程吗?

也许有人遇到过这样的问题并且能够提供帮助。

标签: c++multithreadingmingwclion

解决方案


mingw 无法识别线程库。我将编译器的版本更改为另一个版本,一切正常。


推荐阅读