Code Snippets
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Home :: Programming :: Code Snippets :: Cpp :: Using POSIX Threads

[ Using POSIX Threads ]

During the good old DOS times, multithreading has been praticaly unknown but nowadays nearly every bigger project uses threads, these little nice "tasks" running independently from the main code. On multiprocessor machines there are really running parallel while this is in some way simulated on one-processor computer using task switching. Using threads under Unix system is implemented through the pthreads - POSIX threads - library.

After this little introduction on threads, let's dive into the actual code. I won't discuss all parts of the POSIX library like protecting shared memory but I will rather concentrate on the essential parts say creating and stopping a thread. For further information on threads, try googling as there are enough informations on that topic online.

When using threads in your project, you have to make sure that -lpthread is linked against your application. The main function to create a thread is pthread_create:

int  pthread_create(pthread_t  *  thread,  pthread_attr_t  *
   attr, void * (*start_routine)(void *), void * arg);

thread is a pointer to a pthread_t struct. attr can be used to set the properities of the thread but I will use the default settings, using 0. The third parameter is the startfunction that will be run in the thread declared as void* startfunction(void*). The last parameter is a void pointer that will be the first parameter of your startfunction.

Here's a little snippet creating two independent threads:

#include <iostream>

//should be here..
#define _REENTRANT
#include <pthread.h>

using namespace std;

void* startFunction(void* arg)
   //an endless loop will be started that converts the parameter
   //to a char pointer and outputs it
   	cout << (char*) arg << flush;

int main(int argc, char** argv)
   //two strings that will be used as paramter later on
   char* string1 = "1";
   char* string2 = "2";
   //our two threads
   pthread_t thread1, thread2;
   //our two threads will created with default attributes and the
   //same start function but both with different parameters given
   //to startFunction
   if (pthread_create(&thread1, 0, startFunction, (void*)string1) ||
       pthread_create(&thread2, 0, startFunction, (void*)string2) )
      return 0;

   /* Idle as we don't want to quit now */
   for (;;)
   { }

   return 0;

After compiling the application, you will have two threads running indepentendly from each other, one outputting 1 and the other 2. See how useful the fourth paramter is? ;-). To manually stop a thread, you can either use void pthread_exit(void *retval) from within the thread function or use one of these two functions:

int pthread_join(pthread_t th, void **thread_return);
int pthread_cancel(pthread_t thread);

The difference between the two is that pthread_join will wait until the thread has stopped and so stops the executing code while pthread_cancel sends a cancel request to the thread and immediately returns.

You should have noticed that using threads isn't difficult. After this code snippet you should have got enough knowledge to learn more about the more complex subjects like protecting shared memory. For the lazy fellows among us, try the SPosixThread class from code section. Questions? Message board. Happy Coding!

 Last edited 2003-04-02 20:16:21 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0