【Java 25向量API硬件加速实战指南】:零基础打通AVX-512/SVE指令级优化,3天跑出27.4倍吞吐提升
2026/5/3 18:19:27
https://www.boost.org/doc/libs/1_55_0/doc/html/thread.html
在任何一个时间点上,线程是可结合的(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_interruption与boost::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_