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 |
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.
|
|
|
Goto Forum:
Current Time: Fri Mar 29 07:11:15 CET 2024
Total time taken to generate the page: 0.01157 seconds
|