views:

70

answers:

3

I have the following code which initiate the thread.

int iNMHandleThread = 1;
HANDLE hNMHandle = 0;
hNMHandle = CreateThread( NULL, 0, NMHandle, &iNMHandleThread, 0, NULL);
if ( hNMHandle == NULL)
ExitProcess(iNMHandleThread);

My question is

  1. What will happened if I run this code while the thread already in the running state.
  2. I want to initiate the multiple independent threads of NMHandle kindly give me some hints to solve this problem.
+2  A: 

Each time you call CreateThread, a new thread is started that is independent of any other currently-running threads. Whether your "NMHandle" function is capable of running on more than one thread at a time is up to you: for example, does it rely on any global state?

Dean Harding
@Dean, How can I make NMHandle capable so that it execute multiply at once.
Arman
You don't have to do anything special. However, you will run into problem if your NMHandle accesses global variables and generally assumes there's only one copy running. It's more of a design issue than a programming one though. If you can post the code for your NMHandle function, then we can take a look...
Dean Harding
Every object accessed from `NMHandle` and all functions it calls must be either local or thread-local.
Philipp
@Philipp: or access synchronised with some kind of locking mechanism.
Dean Harding
Agreed with Dean, we have to use some how the synchronized mechanism for access of global objects. Like mu-tax etc.
Arman
A: 

What will happened if I run this code while the thread already in the running state.

Another thread will start with the function NMHandle, independent of the other one.

I want to initiate the multiple independent threads of NMHandle kindly give me some hints to solve this problem.

This code actually creates an independent thread. Create a loop if you want to create multiple threads executing the function NMHandle. If you need the thread handles later (e.g. waiting for a thread to end), you have to store them somewhere.

Make sure that NMHandle is thread-safe. If you don't know what that means, you shouldn't start multithreaded programming yet!

And another hint: You're passing a pointer to the local stack variable iNMHandleThread to the thread. As soon as the function returns, the variable content might not have its expected value anymore - you should rather pass the number by value (CreateThread( NULL, 0, NMHandle, (void*)iNMHandleThread, 0, NULL);).

AndiDog
A: 
  1. CreateThread creates a new thread. The new thread obviously can't be in the running state before - it doesn't have a state before it's created. Compare the simple statement int i = 42; - There's no prior value of i before 42, because the object doesn't exist yet. Obviously the old thread that calls CreateThread() must be running - otherwise it couldn't have run to the line that calls CreateThread() !

  2. Every time you call CreateThread, you will get a new thread. You will also get a new thread handle and ID for every call. So you can't store them all in int iNMHandleThread or HANDLE hNMHandle. Consider a std::list<int> NmThreadIDs and std::list<HANDLE> NmThreadHandles;.

Furthermore, all new threads will start by calling NMHandle(). Is that function thread-safe? That is to say, will that function work properly when executed by two threads at the same time, or interleaved, or in any other random order? Mechanisms like mutexes and critical sections can be used to exclude some unsafe orders of execution.

MSalters