Main Page | Class Hierarchy | Class List | File List | Class Members

PThread example code

Introduction

This page holds an example of the use of PThread and PThreadRun. The examples can be found in the package and show how a method of an object can be called in a seprate thread.

Class PThreadRunTest

In the first example the Test class inherits from PThreadRun and implements the mandatory method 'void run()' which is the starting point for each thread. The main function then shows how to start and join multiple threads.

#include <PThreadRun.h>

#include <iostream>
using namespace std;

class Test : public PThreadRun
{
  void run()
  {
    for (int i=0;i<5;i++)
    {
      cout<<i<<" ";cout.flush();
      sleep(1);
    }
  }
};

int main()
{
  Test test;
  
  int nrThreads=10;
  pthread_t pthread1[nrThreads];

  // start threads
  for (int i=0;i<nrThreads;i++)
    pthread1[i]=test.start();

  sleep(7);

  // join threads
  for (int i=0;i<nrThreads;i++)
    test.join(pthread1[i]);

  return 1;
}

Class PThreadRunTest

In the second example the Test class inherits from PThread which enables it to call more then one method as a seperate thread, in constrast to the previous example. Besides this, these methods also are able to return a void pointer. In this example both testThread1 and testThread2 are methods we call as a seperate thread. We also show the use of a mutex for thread synchronization.

Methods testThread1 and testThread2

After typecasting the void* argument of testThread1 and testThread2 to a pointer of the respective class, variable "me" gives access to the member variables as shown.

As the return type is also a void* we have to keep the pointers target in scope after returning, otherwise the target will be deallocated. Therefore, in this example, I choose to return a pointer to the local static variables 'ret'.

The main function

The main function then shows how to start and join the threads, and how to get the return values.

#include <PThread.h>

#include <iostream>
using namespace std;

class Test: public PThread
{
public:
  Test()
  { pthread_mutex_init(&mutex,NULL);}

  ~Test()
  { pthread_mutex_destroy(&mutex);}

  void setString(string str)
  { s=str;}

  friend void *testThread1(void *t);
  friend void *testThread2(void *t);

private:
  string s;
  pthread_mutex_t mutex;
};

void *testThread1(void *t)
{
  Test *me=(Test *)t;

  pthread_mutex_lock( &(me->mutex) );
  for (int i=0;i<3;i++)
  { cout<<i<<":"<<me->s<<" ";cout.flush();sleep(1);}
  cout<<endl;
  pthread_mutex_unlock( &(me->mutex) );
  
  static int ret=1;
  return &ret;
}

void *testThread2(void *t)
{
  Test *me=(Test *)t;
  
  pthread_mutex_lock( &(me->mutex) );
  for (int i=0;i<3;i++)
  { cout<<i<<":"<<me->s<<" ";cout.flush();sleep(1);}
  cout<<endl;
  pthread_mutex_unlock( &(me->mutex) );
  
  static int ret=2;
  return &ret;
}

int main()
{
  Test t;
  t.setString("SomeText");
  
  cout<<"testThread1 starting"<<endl;
  cout<<"testThread2 starting"<<endl;
  pthread_t pthread1=t.start(testThread1);
  pthread_t pthread2=t.start(testThread2);
  
  // threads are now running

  void *ret1=t.join(pthread1);
  void *ret2=t.join(pthread2);
  cout<<"testThread1 joined with return value: "<<*((int *)ret1)<<endl;
  cout<<"testThread2 joined with return value: "<<*((int *)ret2)<<endl;
}

Generated on Sat Jun 16 14:45:21 2007 for PThread by  doxygen 1.4.2