U++ framework
Do not panic. Ask here before giving up.

Home » U++ Library support » U++ MT-multithreading and servers » Interprocess communication with U++
Re: Interprocess communication with U++ [message #16057 is a reply to message #16056] Mon, 26 May 2008 09:16 Go to previous message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

OK, patch seemes to be trivial.
Here is what I propose:

Mt.h:
class Semaphore {
#ifdef PLATFORM_WIN32
	HANDLE     handle;
#else
	sem_t      sem;
	sem_t     *namedSem;
#endif

public:
	void       Wait();
	void       Release();

	Semaphore();
	Semaphore(const char *name);
	~Semaphore();
}; 


Win32:
void Semaphore::Release()
{
	ReleaseSemaphore(handle, 1, NULL);
}

void Semaphore::Wait()
{
	WaitForSingleObject(handle, INFINITE);
}

Semaphore::Semaphore()
{
	handle = CreateSemaphore(NULL, 0, INT_MAX, NULL);
}

Semaphore::Semaphore(const char *name)
{
	handle = CreateSemaphore(NULL, 0, INT_MAX, name);
}

Semaphore::~Semaphore()
{
	CloseHandle(handle);
}


POSIX:
void Semaphore::Wait()
{
	namedSem ? sem_wait(namedSem) : sem_wait(&sem);
}

Semaphore::Semaphore()
	:namedSem(NULL)

{
	sem_init(&sem, 0, 0);
}

Semaphore::Semaphore(const char *name)
{
	namedSem = sem_open(name, O_CREAT);
}

Semaphore::~Semaphore()
{
	namedSem ? sem_close(namedSem) : sem_destroy(&sem);
}


Didn`t have opportunity to test on Linux.
 
Read Message
Read Message
Read Message
Previous Topic: PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP not defined
Next Topic: WaitForMultipleObjects() analog?
Goto Forum:
  


Current Time: Sat Apr 25 22:22:49 GMT+2 2026

Total time taken to generate the page: 0.00582 seconds