winapi - Win32 - passing data to CreateThread in a "safe" way -
i have code base creates threads ad-hoc in few places following pattern:
- use
operator newcreate struct contain "the stuff" thread wants work with. - call win32
createthread()api. - in thread proc cast lpvoid object
new'ed in step 1. - use object , manually
deleteit.
this works has couple of issues:
- static code analyzers think objected in step 1 can leaked, guess in fact true cases
createthreadfails. - i'd way make more raii/automatic.
- the manual casting can error prone.
i figured std::shared_ptr might solution, e.g in step 1 replace new std::make_shared. seems have other problems:
- i don't think same instance of
std::shared_ptrcan used in other thread? - what if function calls
createthreadexits before thread proc starts, using dangling pointer?
can suggest more elegant , safe way of doing this? other option can think of have class wraps createthread , has std::shared_ptr member , calls lamba thread proc?
Comments
Post a Comment