c++ - How to use std::thread? -
when use std::thread
this:
func() { std::thread(std::bind(&foo, this)); }
the thread object allocated in stack , destroyed when func()
returns. try use like:
func() { std::thread* threadptr = new std::thread(std::bind(&foo, this)); }
where should delete threadptr
? , how can create thread suspended?
if want thread run independently, need use detach()
method on object. otherwise, thread
destructor terminate program if object destroyed while thread still running.
threads start running created. cannot create thread object in suspended state. can either store arguments create thread instead of creating (possibly using, instance, std::function
), or make block on mutex or condition variable until ready let run.