【发布时间】:2021-06-07 01:57:41
【问题描述】:
每个人。 我写的代码如下:
std::thread t1(cmdThread);
t1.detach();
for (int n = 0; n < tCount; n++)
{
std::thread t1(sendThread,n+1);
t1.detach();
}
CmdThread 和 sendThread 是不同的。 所有名为 t1 的线程,但它们正常工作。这让我很困惑。 在 for 循环中,我想创建 4 个线程。 C++是否允许用户创建同名线程并且线程也可以很好的分离?
【问题讨论】:
-
也许你应该看看nested scopes
-
我不知道你说的线程名是什么意思,
t1是一个变量符号,与最终执行的代码完全无关。如果您想查看唯一标识特定线程的内容,您应该查看std::thread::get_id()。 -
是的,伙计。我在函数sendThread中使用了函数std::this_thread::get_id()。 get_id 表明 for 循环中有 4 个不同的线程。
标签: c++ c++11 thread-safety threadpool