- 今天来记录一下多线程,多线程前呢,我们先来一个线程来试一下
#include <iostream>
#include <thread>
using namespace std;
void helloworld()
{
cout << "hello world \n";
}
int main() {
//创建并启动一个线程
thread t (helloworld);
//主线程等待子线程结束,如是没有等待,有可能当子线程还没有结束,主线程就结束了
t.join();
return 0;
}
当运行有以下错误时,请检查你的CMakeLists.txt文件
====================[ Build | threaddemo | Debug ]==============================
/usr/local/clion/clion-2020.1.1/bin/cmake/linux/bin/cmake --build /root/CLionProjects/threaddemo/cmake-build-debug --target threaddemo -- -j 12
-- Configuring done
-- Generating done
-- Build files have been written to: /root/CLionProjects/threaddemo/cmake-build-debug
[ 50%] Linking CXX executable threaddemo
CMakeFiles/threaddemo.dir/main.cpp.o:在函数‘std::thread::thread<void (&)()>(void (&)())’中:
/usr/include/c++/5/thread:137:对‘pthread_create’未定义的引用
collect2: error: ld returned 1 exit status
CMakeFiles/threaddemo.dir/build.make:83: recipe for target 'threaddemo' failed
make[3]: *** [threaddemo] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/threaddemo.dir/all' failed
make[2]: *** [CMakeFiles/threaddemo.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/threaddemo.dir/rule' failed
make[1]: *** [CMakeFiles/threaddemo.dir/rule] Error 2
Makefile:118: recipe for target 'threaddemo' failed
make: *** [threaddemo] Error 2
在CMakeLists.txt文件中加入以下信息:
cmake_minimum_required(VERSION 3.16)
project(threaddemo)
set(CMAKE_CXX_STANDARD 14)
//查找依赖库
find_package(Threads REQUIRED)
add_executable(threaddemo main.cpp ${SOURCE_FILES})
//引入依赖库
target_link_libraries(threaddemo Threads::Threads)
- 创建多个线程并运行
#include <iostream>
#include <thread>
#include <vector>
#include <zconf.h>
using namespace std;
void helloworld()
{
//休眠3s
sleep(3);
cout << "hello world \n";
}
int main() {
vector<thread> threadVector;
for(int i=0;i<10;i++){
//创建并启动一个线程
threadVector.push_back(thread(helloworld));
}
for (auto & threadItem : threadVector)
{
cout << threadItem.get_id() << endl;
threadItem.join();
}
return 0;
}
- 线程终断,当我们启动一个线程,线程中的程序无限制的循环。我们可以使用变量来控制。
#include <iostream>
#include <thread>
#include <vector>
#include <zconf.h>
#include <algorithm>
using namespace std;
bool finsh = false;
void helloworld()
{
//不停去处理某一个事件
for(;;){
sleep(3);
cout << "hello world \n";
if(finsh){
break;
}
}
}
int main() {
vector<thread> threadVector;
for(int i=0;i<10;i++){
//创建并启动一个线程
threadVector.push_back(thread(helloworld));
}
//设置60s后退出
sleep(60);
finsh = true;
//等待每个子线程结束
for_each(threadVector.begin(),threadVector.end(),mem_fn(&std::thread::join));
return 0;
}
- 我们在运行的时候可以随时终断某一个线程,我们这里使用的是boost中的线程。
#include <iostream>
#include <boost/thread.hpp>
#include <boost/format.hpp>
using namespace std;
void helloworld(){
cout << "开始" << endl;
sleep(10);
cout << "结束" << endl;
}
int main() {
boost::thread t(helloworld);
sleep(5);
//当在第5s的时候,我们将它终断
t.interrupt();
printf("主线程结束");
return 0;
}
Boost库的安装:
- 下载源码文件boost_1_65_0.tar.gz
- 载后解压cd至解压文件夹下
cd /Users/jimmy/Downloads/boost_1_65_0
执行
./booststrap.sh
成功后执行
sudo ./b2 install
几分钟之后编译安装完成,这里时间比较长。
编译后的文件位置
头文件位于 /usr/local/include/boost
库路径位于 /usr/local/lib
这篇博客介绍了如何在Ubuntu16.04上使用CLion创建多线程程序,特别提到了在遇到CMakeLists.txt配置问题时的解决方法,并详细讲解了如何利用Boost库进行线程的创建和控制,包括线程的启动、终止和变量同步。

6109

被折叠的 条评论
为什么被折叠?



