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.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -