当前位置: 首页 > news >正文

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_

http://www.cnnetsun.cn/news/61045.html

相关文章:

  • agsXMPP使用
  • HTML图像与多媒体:img、picture、figure、video标签深度解析
  • 区块链 Web3 外包开发公司
  • Claude Code Router智能路由系统:5步实现多AI模型自动调度
  • 3天掌握专业RAW照片处理:darktable零基础快速上手指南
  • CogAgent-9B:2025年视觉语言模型的GUI交互革命
  • Awesome-CV模板完全攻略:打造专业双语简历的终极方案
  • PowerShell自动化运维终极指南:系统管理的革命性工具集
  • 解锁Discord隐藏权限:从用户视角到管理视角的无缝切换
  • 35亿参数重构边缘智能:Liquid AI LFM2-350M开启终端AI效率革命
  • AI产品经理思维揭秘:普通人如何转型成为未来10年最抢手的职业人才!
  • 大语言模型开源突破终极指南:从万亿参数到产业落地
  • 专业的外贸推广电话
  • SimHei字体下载完全指南:轻松解决中文显示难题
  • 基于springboot + vue学习测评系统(源码+数据库+文档)
  • 基于springboot + vue医院管理系统(源码+数据库+文档)
  • Wan2.1视频生成AI:重新定义个人视频创作的新纪元
  • 介绍最近“十大元数据管理工具”
  • 深度学习模型推理加速终极指南:从瓶颈诊断到部署实战
  • 电商系统中的日期转换实战:Date与LocalDate互转
  • 1小时开发:用Pytdx打造简易股票分析看板
  • ms.js终极指南:3分钟掌握JavaScript时间转换技巧
  • 深入Windows系统底层:从注册表到服务开发的进阶指南
  • 归档发票,别再一个一个的整理了!有了它,一次帮你节省一个小时!
  • AI如何帮你高效使用git clone -b命令
  • GRUB引导问题:小白也能懂的修复指南
  • Step3震撼开源:321B参数多模态模型如何重塑AI推理成本与效率
  • WSL跨系统AI服务实战:打通本地大模型与Open Interpreter的无缝连接
  • 30分钟用computeIfAbsent打造缓存系统原型
  • 258M参数引爆文档智能革命:IBM Granite Docling重塑企业内容处理范式