-
仅仅是为了new thread first 吗? |
Beta Was this translation helpful? Give feedback.
Answered by
chenshuo
Jun 9, 2022
Replies: 1 comment 1 reply
-
https://www.zhihu.com/question/294270506/answer/489820748 为了保证 Thread::start() 之后 Thread::tid() 立刻能拿到正确的值。 struct ThreadData
{
void runInThread()
{
*tid_ = muduo::CurrentThread::tid();
tid_ = NULL;
latch_->countDown();
latch_ = NULL; 使用方法: map<pid_t, unique_ptr<Thread>> threads;
Thread* thr = new Thread(...);
thr->start();
threads[thr->tid()].reset(thr); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
yinghaoyu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.zhihu.com/question/294270506/answer/489820748
为了保证 Thread::start() 之后 Thread::tid() 立刻能拿到正确的值。
如果没有 latch_,会有 race condition,即调用 Thread::tid() 的时候线程还没有启动,结果返回初值 0。
使用方法: