2

I'm looking for an API on windows that enables to to create and kill threads at will. Also having ability to bind threads to cores. I was introduced to Win32 Threading API here.
However when I checked MSDN I see _beginthreadex(), and _endthreadex(). So I'm guessing there should be a call to _endthreadex everytime I create a thread?
To get answers to such questions I'm looking for a tutorial on Windows Threading. Can anyone help with this?

P.S. This may be off topic, but does Boost support thread affinity too? If so, can someone point me to a tutorial/documentation related to thread affinity?

Community
  • 1
  • 1
atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • Regarding Boost - see chosen answer on [this related question](http://stackoverflow.com/questions/596411/simple-c-threading) – Liran Orevi Oct 21 '11 at 09:27

3 Answers3

3

Having thread created (such as with _beginthreadex) you need to let the thread exit gracefully as you never know if it is in the middle of something just now (having a lock on a certain resource - for instance). Still you have an option to blow it away with TerminateThread API any time.

SetThreadAffinityMask and friends let you locate your threads at the CPU battlefield. You might end up leaving OS scheduler to choose cores to run your threads on though, as chances are high that it is going to be more efficient.

Update on reusing threads: Creating a thread you are passing your thread proc to start, and as soon as you return from it, the thread is about to be terminated. That is, starting another worker thread activity is possible in two ways: either create a new thread from the start, or do not exit from thread proc and synchronize to catch up a new worker activity request. The latter might be implemented using IPC objects, e.g. events:

int ThreadProc()
{
  while(true)
  {
    wait for new event;
    if(termination requested) break;
    otherwise, on worker activity request, do next requested task;
  }
}

Refer to Thread Synchronization for Beginners for sample code and description.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • you should not use TerminateThread for a thread created with _beginthreadex. Use _endthread. – RED SOFT ADAIR Oct 21 '11 at 10:32
  • If you really care about your code, you should not use `_endthread` either. Instead, you should let the thread proc just complete and return. – Roman R. Oct 21 '11 at 10:40
  • Please, please, please stay away from `SetThreadAffinityMask`. Except for some very rare incidential cases, you will only seriously degrade performance. This is also true for the process affinity mask. The open source game Ryzom is a good example of what happens if someone (someone with no clue) plays with affinities. The game has a dozen or so threads, some from within AL and GL, and they all compete for CPU0. Context switches are plenty, performance and responsiveness abysmal. If you reset affinity with Process Explorer after launch, it immediately runs orders of magnitude better. – Damon Oct 21 '11 at 10:56
  • @Damon: "... leaving OS scheduler to choose... going to be more efficient" that's what it is. Still affinity mask is something on can be on top of. – Roman R. Oct 21 '11 at 11:06
  • `_endthreadex` and `TerminateThread` are the functions that kill the thread externally while it's on the run. You can use the functions, but in most cases it is not a good practice, instead you normally indicate (such as by setting an event) that a thread proc should exist and you wait while the thread finishes itself. This is the reliable way to make sure that you don't kill the thread while it's doing something important. – Roman R. Oct 21 '11 at 14:41
  • @RomanR. Is there a way to re-use threads? I mean create it once and call more than one function with it? – atoMerz Oct 22 '11 at 13:06
  • You start a thread and you are its owner: your thread proc can keep running doing more and more calls ("re-using") or you can return from and it stop the trhead, and then create another one on demand. Both scenarios are appropriate, depending on actual task. – Roman R. Oct 22 '11 at 15:24
  • @RomanR. Thanks, where can I learn these features? It seems that threads are killed once the function is finished (as far as I understood). please add it to your answer and I'll accept it. Thanks again. – atoMerz Oct 24 '11 at 08:43
0

If you are using MFC, you can better use CWinThread. You can send messages to the thread very easily and can control the thread's behaviour from outside. Using the thread's handle, you can provide an affinity mask for a thread using SetThreadAffinityMask, which will schedule a thread on desired processor(s).

Abhinav
  • 1,496
  • 3
  • 15
  • 31
0

1) Do not mix up _beginthread/_beginthreadex and the Win32 API Function CreateThread. These are two different APIs. See Other SO Post for details.

2) If you use _beginthread/_beginthreadex, _endthread/_endthreadex should be used for termination

3) TerminateThread (and also _endthread) should not be used under normal conditions. See MSDN Post.

4) Functions such as SetThreadAffinityMask, or SetThreadIdealProcessor can be used to set the core a thread should use.

5) The boost threading API is much more robust and simple. Actually its the base of the C++11 threads.

Community
  • 1
  • 1
RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92