semaphore.h
Go to the documentation of this file.
00001 #ifndef MT_SEMAPHORE_H
00002 #define MT_SEMAPHORE_H
00003 
00004 #include "base.h"
00005 
00006 #include <iostream>
00007 #include <semaphore.h>
00008 
00009 namespace mt
00010 {
00011 
00012 class semaphore
00013 {
00014   MT_PREVENT_COPY(semaphore)
00015 
00016   public:
00017 
00018   typedef semaphore this_type;
00019   typedef void      base_type;
00020 
00021   semaphore(void)
00022   {
00023     sem_init(&(this->s), 0, 0);
00024   }
00025 
00026   semaphore(int value)
00027   {
00028     sem_init(&(this->s), 0, value);
00029   }
00030 
00031   ~semaphore(void)
00032   {
00033     sem_destroy(&(this->s));
00034   }
00035 
00036   void post(void)
00037   {
00038     sem_post(&(this->s));
00039   }
00040 
00041   /*
00042   void post(int n)
00043   {
00044     sem_post_multiple(&(this->s), n);
00045   }
00046   */
00047 
00048   void wait(void)
00049   {
00050     sem_wait(&(this->s));
00051   }
00052 
00053   bool trywait(void)
00054   {
00055     return (sem_trywait(&(this->s)) == 0);
00056   }
00057 
00058   //methods added for conforming to the QT implementation
00059   //jnoguera 14-12-2011
00060 
00061   void release(int n=1)
00062   {
00063     if(n != 1)
00064       std::cout << "Error, mt::semaphore.release() not supported\n";
00065     sem_post(&(this->s));
00066   }
00067 
00068   void acquire(int n=1)
00069   {
00070     if(n != 1)
00071       std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
00072     sem_wait(&(this->s));
00073   }
00074 
00075   bool tryAcquire(int n=1)
00076   {
00077   if(n != 1)
00078     std::cout << "Error, mt::semaphore.tryAcquire() not supported\n";
00079   return (sem_trywait(&(this->s)) == 0);
00080   }
00081 
00082   int available()
00083   {
00084     int value;
00085     sem_getvalue( &(this->s), &value );
00086     return value;
00087   }
00088 
00089 private:
00090 
00091 sem_t s;
00092 };
00093 
00094 }
00095 
00096 #endif // MT_SEMAPHORE_H


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:35:21