Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
s 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.
Posted

It depends.
Which isn't too helpful, but is true. Yes, two separate thread can execute the same code, that isn't a problem. It's the data that they work on that may cause problems. If they both try to use the same variable outside the procedure then that can indeed cause problems, and you need to handle locking and so forth to ensure it works (and this includes memory passed in as a parameter. If all the memory it uses is allocated locally to the function (i.e. as local non-static variables on the stack, or allocated on the heap within the function) then it should work fine. Google for "Thread safety" and you should find all you need.
 
Share this answer
 
Comments
ayesha hassan 2-May-13 5:06am    
Thank you so very much for the help :)
OriginalGriff 2-May-13 5:13am    
You're welcome!
Quote:
Would the above code create two threads each with same functionality(since thread procedure for both of the threads is same.)
Yes.

Quote:
Am I doing it correctly?
Basically yes. However you shouldn't pass address of i if it is a local variable (and probably you just need its value).
Change
(LPVOID) &i

to
(LPVOID) i

Moreover, I wouldn't overwrite dwThreadId value, use two variables like you did for the thread handles.


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

Of course there are synchronization issues, however they do not depend on thread sharing the same function (each thread have its own copy of local variables). They depend on both threads being allowed to access global variables. So there are synchronization issues between the two threads and between each of them and the main one.
 
Share this answer
 
Comments
ayesha hassan 2-May-13 5:07am    
Thank you so much for such an explanatory answer :)
CPallini 2-May-13 5:09am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900