And*_*uel 7 c++ linux gcc c++11
我的gcc版本4.8.0中的以下代码失败:
#include <thread>
#include <cassert>
int main() {
    std::thread::id nobody;
    assert( nobody != std::this_thread::get_id() );
};
这种行为是否正确?
更新: Jonathan Wakely亲切地看着他说的问题(在评论中),-pthread必须传递给编译器和链接器.如果我这样做,代码也不会失败,使用gcc 4.7.2.所以答案显然与引用的电子邮件无关.谢谢乔纳森!
以下是gcc开发人员Jonathan Wakely在2011年撰写的一些报价: 
我们的std :: thread :: id上的所有比较运算符都依赖于未定义的行为,因为我们的thread :: id只是一个pthread_t.
[...]
2)operator ==使用pthread_equal,对于无效的线程ID未定义,POSIX说:
Run Code Online (Sandbox Code Playgroud)If either t1 or t2 are not valid thread IDs, the behavior is undefined.
奇怪的.以下代码:
#include <iostream>
#include <thread>
int main() {
    std::cout << "Started" << std::endl;
    std::thread::id nobody;
    if ( nobody != std::this_thread::get_id() )  {
      std::cout << "OK" << std::endl;
    }
    std::cout << "Finished" << std::endl;
}
生产:
Started 
OK 
Finished
请点击这里.但是,您的代码确实因4.7.2而失败.
| 归档时间: | 
 | 
| 查看次数: | 2355 次 | 
| 最近记录: |