views:

109

answers:

3

I've been using the Semaphore class to create semaphores. However, the examples use managed code (requires /clr), and I need to use unmanaged code because it seems FreeType doesn't like working with managed code.

How can I create two simple threads which use a semaphore in unmanaged code?

+2  A: 

You may try and use Boost.interprocess. It provides semaphores. See here.

Benoît
+4  A: 

Use native Windows semaphore objects.

Romulo A. Ceccon
Fantastic, exactly what I was looking for but could not find. Thank you.
Dororo
+1  A: 

You want CreateSemaphore which is implemented in kernel32. The general pattern is to create a name or unnamed semaphore object to use from both threads. You can use OpenSemaphore to get a handle to an existing named semaphore. Set the initial count and maximum counts on your semaphore appropriately then use one of the Wait Functions to take a logical lock on your shared resource by decrementing the count on your semaphore. When your thread has finished with the resource, call ReleaseSemaphore to increment the available lock count.

Jim Lamb