Skip to content

Instantly share code, notes, and snippets.

@grk
Created February 7, 2011 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grk/814136 to your computer and use it in GitHub Desktop.
Save grk/814136 to your computer and use it in GitHub Desktop.
#ifndef __semaphore_t_h__
#define __semaphore_t_h__
#ifdef __APPLE__
#include <mach/mach_init.h>
#include <mach/task.h>
#include <mach/semaphore.h>
class semaph_t
{
public:
semaph_t(int initial = 0)
{
semaphore_create(mach_task_self(), &_sem, SYNC_POLICY_FIFO, initial);
}
~semaph_t()
{
semaphore_destroy(mach_task_self(), _sem);
}
void post()
{
semaphore_signal(_sem);
}
void wait()
{
semaphore_wait(_sem);
}
private:
semaphore_t _sem;
};
#else /* __APPLE__ */
#include <semaphore.h>
class semaph_t
{
public:
semaph_t(int initial = 0)
{
sem_init(&_sem, 0, initial);
}
~semaph_t()
{
sem_destroy(&_sem);
}
void post()
{
sem_post(&_sem);
}
void wait()
{
sem_wait(&_sem);
}
private:
sem_t _sem;
};
#endif /* __APPLE__ */
#endif /* __semaphore_t_h__ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment