c++11 std::thread 线程实例在退出后管理线程调用join()后再新建线程将可能会产生相同std::thread::id的实例

[03-28 16:52:54.372] [info] [vthread.cpp:92 operator()()] create new thread,id:4,tid:7f5cbb7fd640,inroduce:test vthread 003
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 1/4 id:1 thread id:0x7f5cbc7ff640,create time:, status:2=running, introduce:"vthread manager"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 2/4 id:2 thread id:0x7f5cbbffe640,create time:, status:3=exit, introduce:"test vthread 002"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 3/4 id:3 thread id:0x7f5cbb7fd640,create time:, status:3=exit, introduce:"test vthread 003"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 4/4 id:4 thread id:0x7f5cbb7fd640,create time:, status:3=exit, introduce:"test vthread 003"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 1/4 id:1 thread id:0x7f5cbc7ff640,create time:, status:2=running, introduce:"vthread manager"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 2/4 id:2 thread id:0x7f5cbbffe640,create time:, status:3=exit, introduce:"test vthread 002"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 3/4 id:3 thread id:0x7f5cbb7fd640,create time:, status:3=exit, introduce:"test vthread 003"
[03-28 16:52:54.372] [info] [vthread.cpp:233 log()] 4/4 id:4 thread id:0x7f5cbb7fd640,create time:, status:3=exit, introduce:"test vthread 003"

 

c++11 std::thread 线程实例在退出后管理线程调用join()后再新建线程将可能会产生相同std::thread::id的实例,这个新线程实例是复用的之前join()释放掉线程实例的旧ID。

所以在用户程序中仅使用std::thread::id 作为线程管理的唯一标识是不够的,往往会导致程序逻辑上错误。

posted @ 2023-03-28 17:32  元几科技  阅读(19)  评论(0编辑  收藏  举报