views:

236

answers:

3

Hi All I am using vc++ and creating a thread using CreateThread function .Can I restart it again once it has returned after completing its task.

+3  A: 

You cannot restart a thread that has terminated. However, you can certainly start a new thread with the same function to run, again with CreateThread.

Martin v. Löwis
thanks a lot lowis
jeromekjeromepune
mark his answer then!
Geo
A: 

Certainly that sounds like loop inside the thread, not restarting it.

Michael Krelin - hacker
+3  A: 

Martin is absolutely correct in stating that you can't restart a thread once it terminated. However, I'd like to point out that for those instances where you know that you are going to need to perform some tasks over and over you should explore what is called "thread pooling".

Essentially, thread pools consist of a number of threads that are alive and waiting for work. When you need to perform a task you simply assign your work task to one such thread and when the thread completes the thread it is put back to a waiting-for-work state. This technique can offer a huge performance boost since creating threads comes with a performance penalty.

For more information on the windows native thread pool you can start here but you may also implement your own thread pools if your application requires functionality that is not available in the Windows Thread Pool API.

As a side note, thread pooling is also supported and highly encouraged in the .NET framework too.

Miky Dinescu