开发者

Semaphores in unmanaged code

I've been using the Semaphore class to c开发者_运维百科reate 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?


Use native Windows semaphore objects.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜