FrozenHeart FrozenHeart - 1 year ago 109
C++ Question

Is it ok to move assign std::thread object from the same thread

Is it ok to do the following?

#include <iostream>
#include <thread>

std::thread th;

void foo()
std::cout << __func__ << std::endl;
th = std::thread(foo);

int main()
th = std::thread(foo);

gcc crashes --

I know that there's almost no need to do this but I want to know the answer just for academic purposes.

Answer Source
th = std::thread(foo);

You're not joining on your thread.

destructs the thread object, underlying thread must be joined or detached

As stated in comments on another answer, assignment has the same requirements as destruction, since the previous thread object is lost.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download