linuxwrap.h

code format="cpp" // linuxwrap.h
 * 1) pragma once


 * 1) include
 * 2) include 
 * 3) include
 * 4) include 
 * 5) include 
 * 6) include 
 * 7) include 
 * 8) include 
 * 9) include

using namespace std;
 * 1) define DWORD void* //unsigned long

class CountingSemaphore { public: CountingSemaphore(unsigned int count); ~CountingSemaphore; void wait; void signal; private: sem_t sem; unsigned int initcount;

}; //--

class Mutex { public: Mutex; ~Mutex; void signal; void wait; private: pthread_mutex_t id; };

//---

class Thread { public:

enum ThreadPriority { PRIORITY_LOW 			= 1, PRIORITY_BELOW_NORMAL 	= 2, PRIORITY_NORMAL 		= 3, PRIORITY_ABOVE_NORMAL	= 4, PRIORITY_HIGH			= 5, };

Thread(ThreadPriority p = PRIORITY_NORMAL, string nme = ""); ~Thread;

void start; bool setPriority(ThreadPriority); ThreadPriority getPriority; string getName;

protected: virtual void run = 0;

private: ThreadPriority priority; string name; pthread_t handle; static DWORD threadMapper(void* p); }; //--

class SleepTimer { public: SleepTimer; ~SleepTimer; void sleep(unsigned long ms); }; //--

class ScopedLocker { public: ScopedLocker(Mutex& m); ~ScopedLocker;

private: Mutex& mutex; }; //--

template class Mailbox { public: Mailbox(const long int cap) : capacity(cap) {		getSemaphore = new CountingSemaphore(0);   // Counts number of items in mailbox putSemaphore = new CountingSemaphore(cap); // Counts number of free slots in mailbox queue = new std::queue; }

~Mailbox {		delete queue; delete putSemaphore; delete getSemaphore; }

void put(const Item& n)	{ putSemaphore->wait; queue->push(n); getSemaphore->signal; }

Item get {		getSemaphore->wait; Item result = queue->front; queue->pop; putSemaphore->signal; return result; }

private: long int capacity; CountingSemaphore* getSemaphore; CountingSemaphore* putSemaphore; std::queue* queue; };

code