issue with Threadsafe singleton with semaphore

后端 未结 2 1905
野的像风
野的像风 2021-01-13 17:03

I have written a simple singleton application.

The following is my sample main class

// ThreadsafeSingletonUsingSemaphore.cpp : Defines the entry poi         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 17:23

    The problem is that call to WaitForMultipleObjects handles up to MAXIMUM_WAIT_OBJECTS which, at least in Visual Studio 2017, is 64.

    Notice how your call to WaitForMultipleObjects to join threads returns WAIT_FAILED.

    In order to wait for more objects one should, according to the documentation:

    To wait on more than MAXIMUM_WAIT_OBJECTS handles, use one of the following methods:

    • Create a thread to wait on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles. Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS.
    • Call RegisterWaitForSingleObject to wait on each handle. A wait thread from the thread pool waits on MAXIMUM_WAIT_OBJECTS registered objects and assigns a worker thread after the object is signaled or the time-out interval expires.

提交回复
热议问题