Question

I have doubt regarding the CreateThread() function in C++.

What is the importance of the threadId parameter in this function?

And are we able to create same thread( same name) using the same threadId. like

     DWORD threadId = 0;
     CreateThread(NULL, 0, Thread1, NULL,0, &threadId);
     CreateThread(NULL, 0, Thread1, NULL,0, &threadId);

This way is possible? What will be the value contain in the threadId? By doing the above codding , Is the second thread creation will over write the threadId value of first thread?

Était-ce utile?

La solution

You don't need the threadId for manipulating the thread if you hold the handle; however, you might choose to retain the IDs instead and then using the handles obtained by OpenThread. (This also works between processes, the only way you can "transfer" a handle to your thread to a different process by OpenThreading the ID there).

You can also use the ID for distributing jobs to your threads (although you can just make up your own identifier).

And last but not least, there are moments you really need the thread ID, such as when calling the PostThreadMessage function.

Autres conseils

What is the importance of the threadId parameter in this function?

If you spawn multiple threads, how do you distinguish between the various threads? This id is the handle that allows you to inspect and process information from the various threads.

This way is possible?

This is possible, but in this specific example you lose the one access point you had to the thread that you created first.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top