winapi - Win32 - passing data to CreateThread in a "safe" way -


i have code base creates threads ad-hoc in few places following pattern:

  1. use operator new create struct contain "the stuff" thread wants work with.
  2. call win32 createthread() api.
  3. in thread proc cast lpvoid object new'ed in step 1.
  4. use object , manually delete it.

this works has couple of issues:

  1. static code analyzers think objected in step 1 can leaked, guess in fact true cases createthread fails.
  2. i'd way make more raii/automatic.
  3. 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:

  1. i don't think same instance of std::shared_ptr can used in other thread?
  2. what if function calls createthread exits 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

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -