news 2026/4/16 13:58:33

Boost C++11多线程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Boost C++11多线程

https://www.boost.org/doc/libs/1_55_0/doc/html/thread.html

thread

  • 当创建一个thread对象后,线程就立刻开始执行。
  • join()和timed_join()方法等待线程结束。
    • join()一直阻塞等待,直到线程结束。
    • timed_join()阻塞等待线程结束,或阻塞等待一定的时间段,然后不管线程是否结束都返回。
  • detach()与线程执行体分离,线程执行体不受影响的继续执行直到运行结束。
  • 可以使用bind()和function库。
  • thread类的3个静态成员函数:
    • yield() 指示当前线程放弃时间片,允许其他的线程运行。
    • sleep() 让线程睡眠等待一小段时间。
    • hardware_concurrency() 获得硬件系统可并行的线程数量,即CPU数量。
  • thread::this_thread 子命名空间:
    • get_id() 获得线程ID
    • yield() 放弃时间片
    • sleep() 睡眠等待
    • at_thread_exit(func)函数,允许“登记”一个线程在结束的时候执行可调用物func,无论线程是否被中断。
  • interrupt() 中断线程,允许正在执行的线程被中断,被中断的线程会抛出一个thread_interrupted异常。

创建线程4种方法以及分离或等待线程

在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached)。

void my_func() { std::cout << "detach-分离 或 join-等待 线程" << std::endl; } TEST(BoostThread, detachOrjoin)//简单线程,线程状态joinable、detached { boost::thread t(my_func); boost::thread::yield();//当前线程放弃余下的时间片。 std::cout << t.joinable() << std::endl; t.join(); boost::thread t1(my_func); std::cout << t1.joinable() << std::endl; t1.detach(); std::cout << t1.joinable() << std::endl; } //线程的创建需要传递给thread对象一个可调用物(函数或函数对象),它必须具 //有operator()以供线程执行。 boost::mutex io_mutex; struct count { count(int id) : id(id) { } void operator()() { for (int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(io_mutex); std::cout << id << ": " << i << std::endl; } } int id; }; TEST(BoostThread, Typeobject)//复杂类型对象作为参数来创建线程 { boost::thread thrd1(count(1)); boost::thread thrd2(count(2)); thrd1.join(); thrd2.join(); } class HelloWorldStatic { public: static void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } static void start() { boost::thread thrd(hello); thrd.join(); } }; TEST(BoostThread, InClassStatic)//类内部创建线程 { HelloWorldStatic::start();//在这里start()和hello()方法都必须是static方法。 } class HelloWorld { public: void hello() { std::cout << "Hello world, I''m a thread!" << std::endl; } void start() { boost::function0< void> f = boost::bind(&HelloWorld::hello, this); boost::thread thrd(f); thrd.join(); } }; TEST(BoostThread, InClass)//start()和hello()方法不是静态方法则采用此方法创建线程 { HelloWorld hello; hello.start(); } class HelloWorldOut { public: void hello(const std::string& str) { std::cout << str; } }; TEST(BoostThread, OutClass) { HelloWorldOut obj; boost::thread thrd(boost::bind(&HelloWorldOut::hello, &obj, "Hello world, I''m a thread!" ) ) ; thrd.join(); }

线程的参数传递

void func1(const int &id) { std::cout << "func1 id : " << id << std::endl; } struct MyThread { void operator()(const int &id) { std::cout << "MyThread id : " << id << std::endl; } void func1(const int &id) { std::cout << "MyThread::func1 id : " << id << std::endl; } }; TEST(BoostThread, Threadparameters) { //普通函数 boost::thread t1(func1, 11); t1.join(); //函数对象 MyThread myThread; boost::thread t2(myThread, 22); t2.join(); //成员函数 boost::thread t3(&MyThread::func1, myThread, 33); t3.join(); //临时对象 boost::thread t4(MyThread(), 44); t4.join(); //对象引用 boost::thread t5(boost::ref(myThread), 55); t5.join(); }

还可使用bing与ref

线程中断

禁用于回复中断:
boost::this_thread::disable_interruptionboost::this_thread::restore_interruption

void f() { // interruption enabled here { boost::this_thread::disable_interruption di; // interruption disabled { boost::this_thread::disable_interruption di2; // interruption still disabled } // di2 destroyed, interruption state restored // interruption still disabled } // di destroyed, interruption state restored // interruption now enabled } void g() { // interruption enabled here { boost::this_thread::disable_interruption di; // interruption disabled { boost::this_thread::restore_interruption ri(di); // interruption now enabled } // ri destroyed, interruption disable again } // di destroyed, interruption state restored // interruption now enabled }

函数检测当前线程是否允许中断:boost::this_thread::interruption_enabled()

函数检测当前线程是否被要求中断:boost::this_thread::interruption_requested()

disable_interruption是一个RAII类型的对象,它在构造时关闭线程的中断,析构时自动恢复线程的中断状态。在disable_interruption的生命期内线程始终是不可中断的,除非使用了restore_interruption对象。

restore_interruption只能在disable_

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 7:21:57

61、Linux 打印系统配置全攻略

Linux 打印系统配置全攻略 1. 运行打印系统 Linux 打印系统以守护进程的形式运行,在使用前必须先启动。通常,这一任务会通过 /etc/rc.d 或 /etc/rc?.d (其中 ? 是运行级别编号)中的启动脚本来自动处理。可以通过查找名称中包含 lpd 、 lprng 或 cups 的启动…

作者头像 李华
网站建设 2026/4/15 14:26:04

59、网络基础配置与诊断全解析

网络基础配置与诊断全解析 1. PPP配置工具 在进行PPP(Point-to-Point Protocol)配置和使用时,有不少工具可供选择。其中比较突出的是图形用户界面(GUI)工具,例如KPPP,它是K桌面环境(KDE)的一部分。这些程序允许用户通过类似于Windows的点选式界面来管理PPP会话。此外…

作者头像 李华
网站建设 2026/4/16 7:29:59

Spring Boot 整合 RocketMQ 实战:消息发送、消费、重试全流程代码示例

RocketMQ 作为阿里开源的分布式消息中间件&#xff0c;凭借高吞吐量、低延迟、高可靠性等特性&#xff0c;被广泛应用于分布式系统的异步通信、解耦、削峰填谷等场景。Spring Boot 作为主流的微服务开发框架&#xff0c;其自动配置机制能极大简化与第三方组件的整合过程。本文将…

作者头像 李华
网站建设 2026/4/16 7:20:39

数智赋能城轨运营:国际前沿与本土实践探索及未来展望

摘要&#xff1a; 全球范围内&#xff0c;以大数据、人工智能、物联网、数字孪生为代表的数智技术正深刻重塑城市轨道交通的运营范式。本文从全球视角出发&#xff0c;系统剖析了新加坡、伦敦、巴黎等国际先进城轨系统在集成化智能调度、预测性维护、乘客体验革新等方面的前沿实…

作者头像 李华
网站建设 2026/4/16 7:21:42

MFC Split Button Control 完全指南:从入门到精通

一、Split Button Control 概述 Split Button Control 是 MFC 中的一个组合控件,它由一个主按钮和一个下拉菜单按钮组成。主按钮用于执行默认操作,而下拉菜单按钮用于选择其他操作选项。这种控件在提交操作、导出操作和文件操作等场景中具有广泛的应用,能够提升界面的视觉一…

作者头像 李华
网站建设 2026/4/16 7:21:48

前端如何实现分页?零基础入门到精通,收藏这篇就够了

先定义分页中需要用的三个值&#xff1a;currentPage&#xff08;当前页码&#xff09;、total&#xff08;总条数&#xff09;、pageSize&#xff08;每页展示的数据量&#xff09; 分页的思路&#xff1a;把所有的数据请求回来后&#xff0c;通过arr.slice(开始索引&#xff…

作者头像 李华