Edgar Huckert

Threads under Windows and Linux

Concurrent programming can be achieved in modern operating systems using several concepts: Many operating systems - especially those offering GUI capabilities - use additionally other mechanisms like timer based callback routines or event based mechanisms to achieve a part of the problems appearing in cocurrent programming.

We concentrate here on concurrent threads. Threads are normally not restricted to GUI environments - they can also be used in traditional console applications or in server applications (among them CGI programs). Programming with threads is one of the crucial techniques in real time systems. Although very similar in the concept, the APIs under Windows and Unix are completely different. Meanwhile threads are also part of some programming languages (see the libraries coming with Java, D etc.) and of some portable libraries like wxWidgets or Boost.

Around 2001 the POSIX comittee released a concept called "Portable threads" (PThreads). The POSIX standard also includes portable methods (mutexes etc.) for the access to shared ressources. These PThreads can be used under Unix/Linux and Windows, although not all Windows C compilers offer libraries for PThreads. The newer GNU gcc compiler has such a library under Windows and under Linux. Both architectures - Windows and Linux - have also complete specific sets of API methods for the use of threads.

My sample program threads2.c shows a simple portable solution for a common problem: how to wait for the termination of multiple threads in the main program. I use here simple mutexes in combination with the API call pthread_mutex_trylock(). My simple solution even does not lock any shared variables. Note that other solutions using pthread_join() don't work if you have to wait for multiple threads as these calls block.

You will find a quite complex but portable application using threads (POSIX and Windows) in my ZIP file for my TCP server. A more general solution is discussed in my (German) article under Real time systems. This solution uses queues for the communication between threads and the main program (the controlling thread).

Another sample program (includes the source code) demonstrating the use of threads under Windows 32 bit can be found on this site under the name Setting the audio volume in Windows using parallel threads.


If you want to contact me: this is my
mail address