简体   繁体   English

突破Boost.Thread 3.0.0中的变化

[英]Breaking changes in Boost.Thread 3.0.0

In the release notes of version 1.50.0 of the Boost libraries I noted two breaking changes ( see here ): 在Boost库1.50.0版的发行说明中,我注意到两个重大变化( 见这里 ):

#6266 Breaking change: thread destructor should call terminate if joinable. #6266断开变化:如果可连接,线程析构函数应该调用terminate。

#6269 Breaking change: thread move assignment should call terminate if joinable. #6269中断更改:线程移动分配应该在可连接时调用terminate。

What does this mean for my existing projects currently using Boost 1.49.0? 这对我目前使用Boost 1.49.0的现有项目意味着什么? Do I have to change anything? 我需要改变什么吗? If yes, what do I have to change exactly? 如果是的话,我该怎么做才能完全改变? And what happens if I forget to modify one of my existing projects? 如果我忘记修改我现有的一个项目会怎样? Will I get compile time errors (I hope so) or will I get nasty and hard-to-find runtime problems (I absolutely don't hope so)? 我是否会遇到编译时错误(我希望如此)或者我会遇到令人讨厌且难以发现的运行时问题(我绝对不希望如此)?

When it says "Breaking change", it means, "Your program is broken if you depend on behaviour which was previously X, but is now Y". 当它说“突破变化”时,它意味着,“如果你依赖于先前X的行为,你的程序就会被破坏,但现在是Y”。

For the given two changes, it means that if you rely on the destructor or move assignment calling join() (or detach() ), which I believe was the previous behaviour, your program must now explicitly join() or detach() or meet your friend std::terminate() . 对于给定的两个更改,这意味着如果您依赖析构函数或移动赋值调用join() (或detach() ),我认为这是以前的行为,您的程序现在必须显式join()detach()或认识你的朋友std::terminate() It's not a compile-time error, but nor is it unpredictable run-time behaviour- you'll get a nice clean crash leading right back to boost::thread 's destructor, which is the cause of the problem. 这不是编译时错误,但也不是不可预测的运行时行为 - 你会得到一个很好的干净崩溃,直接回到boost::thread的析构函数,这是导致问题的原因。

The following code used to work correctly, but with v3 the program would be aborted as t leaves its scope, because thread::~thread calls std::terminate , instead of silently detaching from the thread: 以下代码用于正常工作,但是对于v3,程序将在t离开其作用域时中止,因为thread::~thread调用std::terminate ,而不是从线程中静默分离:

#include <boost/thread.hpp> 
#include <iostream>

void f()
{}

int main() 
{ 
  {
    boost::thread t(f);
  }
  std::cout << "exiting gracefully" << std::endl;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM