0

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.

hmjd
  • 120,187
  • 20
  • 207
  • 252
Ayse
  • 2,676
  • 10
  • 36
  • 61
  • Start here: http://en.wikipedia.org/wiki/Thread_(computing) – Klas Lindbäck May 02 '13 at 08:26
  • 1
    Your second thread id will overwrite the first. If you don't care about thread ids, just pass `NULL`. If you care, use `dwThreadId1` and `dwThreadId2` (or an array, of course). – MSalters May 02 '13 at 11:07
  • Clear duplicate of [How to create multiplethreads each with different ThreadProc() function using CreateThread()](http://stackoverflow.com/questions/16331478/how-to-create-multiplethreads-each-with-different-threadproc-function-using-cr) which you asked at the same time. – Raymond Chen May 02 '13 at 13:48
  • Remove those casts (all casts in general), they only serve to hide bugs. You neither need to cast the function pointer to the entry function nor the pointer to context, provided they have the right type. If they don't, fix that. – Ulrich Eckhardt May 02 '13 at 18:19

4 Answers4

6

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.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • @AyeshaHassan, do not edit the question in response to answers. This makes it confusing for people who later come to the question and makes the posted answers appear incorrect or unhelpful. Suggest searching for a question related to "passing thread arguments" or post a new question. – hmjd May 02 '13 at 09:04
  • I got it. Thank you so much for the explanataion. – Ayse May 02 '13 at 09:08
5

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.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
4

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.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

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.

nkuzbp
  • 21
  • 3