C++ 获取std::thread';s线程:运行前的id?

C++ 获取std::thread';s线程:运行前的id?,c++,multithreading,stdthread,C++,Multithreading,Stdthread,我试图在C++ 11的代码> STD::线程< /COD>上构建线程安全层,其中每个对象被分配给一个拥有的线程,并且某些调用在错误线程上使用时会引起一个硬错误。拥有线程是唯一可以将对象传输到另一个线程的线程 我已经让它全部正常工作了,只是在它实际运行之前,我找不到一种方法来获取线程的thread::id。我需要将新线程的ID附加到对象上,然后再将其移交 如果我使用 std::thread newThread( [theObject]() {

我试图在C++ 11的代码> STD::线程< /COD>上构建线程安全层,其中每个对象被分配给一个拥有的线程,并且某些调用在错误线程上使用时会引起一个硬错误。拥有线程是唯一可以将对象传输到另一个线程的线程

我已经让它全部正常工作了,只是在它实际运行之前,我找不到一种方法来获取线程的
thread::id
。我需要将新线程的ID附加到对象上,然后再将其移交

如果我使用

std::thread newThread( [theObject]()
                       {
                           // use theObject here.
                       } );
我能得到线程ID的最早点是在thread对象的定义之后,此时线程已经在运行了

我看到了
std::thread
有一个默认的构造函数,但是我看不到一种方法来给它一个函数,以便在以后在线程上运行

有没有一种方法可以在线程上执行两步构造,或者在创建线程时控制线程的ID?

否--只要创建线程,它就会开始运行。如果您想在它做(大部分)任何事情之前获得它的ID,您可能想创建一个小包装器,在那里您传递线程(例如)一个CV和一个队列,它将输出存放在那里

然后,当线程启动时,它检索自己的ID,将其存放在输出队列中,然后等待CV。当父级检索到ID,并准备好让子级开始执行某项操作时,它会向CV发送信号,然后关闭CV。

否--一旦创建线程,它就会开始运行。如果您想在它做(大部分)任何事情之前获得它的ID,您可能想创建一个小包装器,在那里您传递线程(例如)一个CV和一个队列,它将输出存放在那里


然后,当线程启动时,它检索自己的ID,将其存放在输出队列中,然后等待CV。当家长检索了ID,并准备好让孩子开始做某事时,它发出了CV的信号,然后关闭它。

< P>而不是在开始运行之前获取线程的ID,您可以考虑在执行之前执行线程执行的一些初始设置。例如,您可以这样做:

bool isReady = false;
bool wasReceived = false;
std::mutex mutex;
std::condition_variable condition;

std::thread newThread([theObject, &isReady, &mutex, &condition] {
   /* Wait until we've been cleared to go. */
   std::unique_lock<std::mutex> lock(isReady);
   condition.wait(lock, [&isReady] { return isReady; });

   /* Signal that we're done. */
   wasReceived = true;
   lock.unlock();
   condition.notify_one();

   /* Put code here to do whatever it is that the thread should do. */
});

/* Read the thread's ID. It's currently waiting for us. */
auto id = newThread.get_id();

/* Tell the thread that we're ready for it. */
std::unique_lock<std::mutex> lock(mutex);
isReady = true;
condition.notify_one();

/* Wait until the thread has confirmed that it's ready. */
condition.wait(lock, [&] { return wasReceived; });
bool isReady=false;
bool wasReceived=false;
std::互斥互斥;
std::条件\可变条件;
std::thread newThread([theObject、&isReady、&mutex、&condition]{
/*等我们被允许离开*/
std::唯一锁(isReady);
等待(锁,[&isReady]{return isReady;});
/*发出信号,我们结束了*/
wasreived=true;
lock.unlock();
条件。通知_one();
/*将代码放在这里,以执行线程应该执行的任何操作*/
});
/*读取线程的ID。它当前正在等待我们*/
auto id=newThread.get_id();
/*告诉线程我们已经准备好了*/
std::唯一锁(互斥锁);
isReady=真;
条件。通知_one();
/*等待线程确认它已准备就绪*/
wait(lock,[&]{return wasReceived;});
这将创建线程,并让它坐在那里等待,直到创建者有机会读取其ID。一旦发生这种情况,创建者将等待线程确认它已准备就绪,然后您可以根据自己的喜好使用线程ID


注意上面代码中的错误-它完全未经测试。:-)
bool isReady = false;
bool wasReceived = false;
std::mutex mutex;
std::condition_variable condition;

std::thread newThread([theObject, &isReady, &mutex, &condition] {
   /* Wait until we've been cleared to go. */
   std::unique_lock<std::mutex> lock(isReady);
   condition.wait(lock, [&isReady] { return isReady; });

   /* Signal that we're done. */
   wasReceived = true;
   lock.unlock();
   condition.notify_one();

   /* Put code here to do whatever it is that the thread should do. */
});

/* Read the thread's ID. It's currently waiting for us. */
auto id = newThread.get_id();

/* Tell the thread that we're ready for it. */
std::unique_lock<std::mutex> lock(mutex);
isReady = true;
condition.notify_one();

/* Wait until the thread has confirmed that it's ready. */
condition.wait(lock, [&] { return wasReceived; });
bool isReady=false;
bool wasReceived=false;
std::互斥互斥;
std::条件\可变条件;
std::thread newThread([theObject、&isReady、&mutex、&condition]{
/*等我们被允许离开*/
std::唯一锁(isReady);
等待(锁,[&isReady]{return isReady;});
/*发出信号,我们结束了*/
wasreived=true;
lock.unlock();
条件。通知_one();
/*将代码放在这里,以执行线程应该执行的任何操作*/
});
/*读取线程的ID。它当前正在等待我们*/
auto id=newThread.get_id();
/*告诉线程我们已经准备好了*/
std::唯一锁(互斥锁);
isReady=真;
条件。通知_one();
/*等待线程确认它已准备就绪*/
wait(lock,[&]{return wasReceived;});
这将创建线程,并让它坐在那里等待,直到创建者有机会读取其ID。一旦发生这种情况,创建者将等待线程确认它已准备就绪,然后您可以根据自己的喜好使用线程ID


注意上面代码中的错误-它完全未经测试。:-)

通过传递唯一的std::promise参数来启动每个未激活的线程,首先获取线程id(线程id用作传递引用参数),然后让它等待线程管理器设置承诺。这也将消除使用条件变量的麻烦

已编辑片段

class smart_thread {
public:
    smart_thread(std::function<void(void)> task)
    {
        thread_ = std::thread([=]() {
            id_ = std::this_thread::get_id();
            // wait here till activated
            future_.get();
            if(active_) task();
        });
    }

    void activate()  {
        promise_.set_value();
        active_ = true;
    }

    ~smart_thread() {
        if(!active_) promise_.set_value();
        thread_.join();
    }    
private:
    std::thread::id id_;
    std::atomic<bool> active_ = false;
    std::thread thread_;
    std::promise<void> promise_;
    std::future<void> future_ = promise_.get_future();
};

void main()
{
    auto task = []() { std::cout << "Hello World\n"; };
    smart_thread thread(task); // start thread inactive mode
    thread.activate(); // activate thread
}
class智能线程{
公众:
智能线程(std::函数任务)
{
线程=std::线程([=](){
id=std::this_thread::get_id();
//在这里等到激活
future_.get();
如果(活动)任务();
});
}
无效激活(){
承诺。设置值();
主动=真;
}
~smart_thread(){
如果(!active_uu)promise_u.set_value();
螺纹连接();
}    
私人:
std::thread::id_389;;
std::原子活动=假;
标准:螺纹螺纹;
std::承诺-承诺;
std::future-future_uu=承诺_uu.get_-future();
};
void main()
{

auto task=[](){std::cout通过传递唯一的std::promise参数来启动每个非活动线程,首先获取线程id(线程id用作传递引用参数),然后让它等待线程管理器设置承诺