Question

Is it possible for two threads to use a single function "ThreadProc" as its thread procedure when CreateThread() is used?

HANDLE thread1= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
                              (LPVOID) &i, //Thread parameter
                              0, //Immediately run the thread
                              &dwThreadId //Thread Id
                            ) 
HANDLE thread2= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
                              (LPVOID) &i, //Thread parameter
                              0, //Immediately run the thread
                              &dwThreadId //Thread Id
                            ) 

Would the above code create two threads each with same functionality(since thread procedure for both of the threads is same.) Am I doing it correctly?

If it is possible then would there be any synchronization issues since both threads are using same Thread Procedure.

Please help me with this. I am really confused and could not find anything over the internet.

Was it helpful?

Solution

It is fine to use the same function as a thread entry point for multiple threads.

However, from the posted code the address of i is being passed to both threads. If either thread modifies this memory and the other reads then there is a race condition on i. Without seeing the declaration of i it is probably a local variable. This is dangerous as the threads require that i exist for their lifetime. If i does not the threads will have a dangling pointer. It is common practice to dynamically allocate thread arguments and have each thread free its arguments.

OTHER TIPS

Yes, it is very well possible to have multiple (concurrent) threads that start with the same entry point. Apart from the fact that the OS/threading library specifies the signature and calls it, there is nothing special about a thread entry point function. It can be used to start off multiple threads with the same caveats as for calling any other function from multiple threads: you need synchronization to access non-atomic shared variables.

Each thread uses its own stack area, but that gets allocated by the OS before the Thread Procedure get invoked, so by the time the Thread Procedure gets called all the special actions that are needed to create and start a new thread have already taken place.

Whether the threads are using the same code or not is irrelevant. It has no effect whatsoever on synchronization. It behaves precisely the same as if they were different functions. The issues with potential races is the same.

You probably don't want to pass both threads the same pointers. That will likely lead to data races. (Though we'd have to see the code to know for sure.)

Your code is right. There is NOT any synchronization issues between both threads. If they need synchronization, it maybe because they are change the same global variable, not because they use the same thread Procedure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top