Subj : Re: regarding threads in VC++, for a beginnner To : comp.os.ms-windows.programmer.win32,comp.programming.threads From : Scott McPhillips [MVP] Date : Sat Jan 22 2005 09:14 am Ian T wrote: > Scott McPhillips [MVP] wrote: >> The recommended way to start such a "worker" thread is to call >> _beginthreadex. One of its parameters is a function pointer to your >> thread function. > > Another parameter (void*) permits you to pass data to > >> the thread function. Then your thread function runs concurrently. >> > I have been taught (by someone else) to pass an object reference in > lparam (as a void *), then convert it back to the object and call a > member function. > > So my questions are these. > 1. Is this bad practice? Should I push my objects onto a queue and > process them in the thread funtion (dealing with contention for the > queue with a mutex or semaphore)? > > 2. Does the member function that I call in the thread function execute > in the thread's "space"? Or does it execute in in the main thread's > "space"? > > Thanks, > Ian The void* thread parameter can be used to pass a pointer to any desired data structure, including an object reference. Converting it back to the object so you can call a C++ member function is not bad practice, it is a good C++ technique. Passing the void* is only possible at thread start. If you want to pass data to the thread more than once then you need an additional mechanism, such as a queue or passing messages in the Windows queue. Queues are not a replacement for the thread parameter: Each serves a different purpose. If "space" means memory space: all threads in a process use the same memory space. If this were not so the void* thread parameter would be useless. -- Scott McPhillips [VC++ MVP] .