/*************************************************************************** rlthread.h - description ------------------- begin : Tue Jan 02 2001 copyright : (C) 2001 by R. Lehrig email : lehrig@t-online.de ***************************************************************************/ /*************************************************************************** * * * This library is free software; you can redistribute it and/or modify * * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as * * published by the Free Software Foundation * * * ***************************************************************************/ #ifndef _RL_THREAD_H_ #define _RL_THREAD_H_ #include "rldefine.h" #include "rlwthread.h" class rlThread; /*!
This parameter is given to the thread function.
*/ typedef struct { rlThread *thread; void *user; int running; } THREAD_PARAM; /*!
Thread functions based on POSIX threads.
*/ class rlThread { public: rlThread(int max_semphore=1000); virtual ~rlThread(); /*!
  Create a new thread
  Your thread function looks like:

  void *threadFunction(void *arg)
  {
    THREAD_PARAM *p = (THREAD_PARAM *) arg;
    YOUR_DATA    *d = (YOUR_DATA *) p->user;

    for(int i=0; i<50; i++)
    {
      p->thread->lock();
      // do something critical
      printf("this is the thread\n");
      p->thread->unlock();
    }

    return NULL;
  }
  
*/ int create(void *(*func)(void*), void *argument); /*!
  Try to lock the mutex.
  return 0 if already locked
  return !0 if lock sucessfull
  
*/ int trylock(); /*!
  Lock the mutex.
  
*/ int lock(); /*!
  Unlock the mutex.
  
*/ int unlock(); /*!
  Wait until semaphore is signaled
  
*/ int waitSemaphore(); /*!
  Increment the value of the semaphore
  
*/ int incrementSemaphore(); /*!
  Wait for termination of thread and get the exit status
  
*/ int join(void **status); /*!
  Cancel the thread
  
*/ int cancel(); /*!
  Terminate the thread and return exit status
  
*/ void threadExit(void *status); pthread_t tid; pthread_attr_t attr; pthread_mutex_t mutex; WSEMAPHORE semaphore; private: THREAD_PARAM arg; }; /*!
Mutex functions based on POSIX threads.
*/ class rlMutex { public: //rlMutex(const pthread_mutexattr_t *attr = NULL); rlMutex(const void *attr = NULL); virtual ~rlMutex(); /*!
  Try to lock the mutex.
  return 0 if already locked
  return !0 if lock sucessfull
  
*/ int trylock(); /*!
  Lock the mutex.
  
*/ int lock(); /*!
  Unlock the mutex.
  
*/ int unlock(); pthread_mutex_t mutex; }; /*!
Semaphore functions based on POSIX threads.
*/ class rlSemaphore { public: rlSemaphore(int max_semaphore = 1000); virtual ~rlSemaphore(); /*!
  Wait until semaphore is signaled
  
*/ int waitSemaphore(); /*!
  Increment the value of the semaphore
  
*/ int incrementSemaphore(); WSEMAPHORE semaphore; }; #endif