WaitForSingleObject
-
I have a COm framework where I have two classes A and B that service requests based on their respective queues . for e.g life Sequence for Class A is as follows : 1.Class A object created 2.AddedToQueue_A event created as nonSignalled . 3.Worker thread within Class A created . 4.CLass A QUEUE is empty hence Worker thread is blocked with the call:
ResetEvent(AddedToQueue_A) WaitForSingleObject(AddedToQueue_A,INFINITE);
5. When a request is added to Queue(Enqueue) a call is made to awaken the thread like : SetEvent(AddedtoQueue_A); 6. Thread A is awakened and the request is serviced . Similar is the case of Class B object Observations : 1. With Class A object present and when there is no Class B object the thread is awakened properly and the queue is serviced . 2. With Class A and CLass B objects present , the observation is that though an entry is added to the Queue of class A , Class A thread is not getting awakened everytime for some reason .the thread gets awakened every alternated time when the Queue size =2; My question is that is the blocking statement WaitForsingleObject() associated with the thread priority . Why isnt Class A's thread being called everytime I Set the event while enqueuing ? -
I have a COm framework where I have two classes A and B that service requests based on their respective queues . for e.g life Sequence for Class A is as follows : 1.Class A object created 2.AddedToQueue_A event created as nonSignalled . 3.Worker thread within Class A created . 4.CLass A QUEUE is empty hence Worker thread is blocked with the call:
ResetEvent(AddedToQueue_A) WaitForSingleObject(AddedToQueue_A,INFINITE);
5. When a request is added to Queue(Enqueue) a call is made to awaken the thread like : SetEvent(AddedtoQueue_A); 6. Thread A is awakened and the request is serviced . Similar is the case of Class B object Observations : 1. With Class A object present and when there is no Class B object the thread is awakened properly and the queue is serviced . 2. With Class A and CLass B objects present , the observation is that though an entry is added to the Queue of class A , Class A thread is not getting awakened everytime for some reason .the thread gets awakened every alternated time when the Queue size =2; My question is that is the blocking statement WaitForsingleObject() associated with the thread priority . Why isnt Class A's thread being called everytime I Set the event while enqueuing ?I don't entirely get your design, but I guess it doesn't matter, because:
ResetEvent(AddedToQueue_A)
WaitForSingleObject(AddedToQueue_A,INFINITE);is fundamentally flawed. Let me explain. Suppose a request is enqueued into A when worker thread A is servicing a prior request and before it has completed to do so. When the worker thread fininshes, it resumes waiting, but before that it calls
ResetEvent(AddedToQueue_A)
: the signal is lost and the thread sits here waiting for ever (or until another request with more lucky timing gets in). Instead, createAddedtoQueue_A
as automatic (see the docs forCreateEvent
) and do not useResetEvent
. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
I don't entirely get your design, but I guess it doesn't matter, because:
ResetEvent(AddedToQueue_A)
WaitForSingleObject(AddedToQueue_A,INFINITE);is fundamentally flawed. Let me explain. Suppose a request is enqueued into A when worker thread A is servicing a prior request and before it has completed to do so. When the worker thread fininshes, it resumes waiting, but before that it calls
ResetEvent(AddedToQueue_A)
: the signal is lost and the thread sits here waiting for ever (or until another request with more lucky timing gets in). Instead, createAddedtoQueue_A
as automatic (see the docs forCreateEvent
) and do not useResetEvent
. Good luck. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloThank you for your insight . I made the modification but still find that my thread (Class A)gets awakened every alternate request (i.e Queuesize=2) . This behaviour is absent when the other thread of class B is absent . here is a snippet of my thread operation
while(threadNeedstorun){ if(queueSize>0){ //Service Queue } // Block WaitForSingleObject(AddedtoQueue_A,INFINITE) // Awake } here is the snippet of my Enqueue operation if(queueSize
-
Thank you for your insight . I made the modification but still find that my thread (Class A)gets awakened every alternate request (i.e Queuesize=2) . This behaviour is absent when the other thread of class B is absent . here is a snippet of my thread operation
while(threadNeedstorun){ if(queueSize>0){ //Service Queue } // Block WaitForSingleObject(AddedtoQueue_A,INFINITE) // Awake } here is the snippet of my Enqueue operation if(queueSize
OK, this kind of stuff is always somewhat messy. Try this. Define a
CRITICAL_SECTION
, sayCS_A
(don't forget to initialize it withInitializeCriticalSection
), createAddedtoQueue_A
as manual and use the following for incrementing and decrementing the number of elements in the queue:void DecrementElementsInA()
{
EnterCriticalSection(CS_A);
--queueSize;
if(queueSize==0){
ResetEvent(AddedtoQueue_A);
}
LeaveCriticalSection(CS_A);
}IncrementElementsInA()
{
EnterCriticalSection(CS_A);
++queueSize;
if(queueSize>0){
SetEvent(AddedtoQueue_A);
}
LeaveCriticalSection(CS_A);
}Think this sould do. Please report back, regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
OK, this kind of stuff is always somewhat messy. Try this. Define a
CRITICAL_SECTION
, sayCS_A
(don't forget to initialize it withInitializeCriticalSection
), createAddedtoQueue_A
as manual and use the following for incrementing and decrementing the number of elements in the queue:void DecrementElementsInA()
{
EnterCriticalSection(CS_A);
--queueSize;
if(queueSize==0){
ResetEvent(AddedtoQueue_A);
}
LeaveCriticalSection(CS_A);
}IncrementElementsInA()
{
EnterCriticalSection(CS_A);
++queueSize;
if(queueSize>0){
SetEvent(AddedtoQueue_A);
}
LeaveCriticalSection(CS_A);
}Think this sould do. Please report back, regards, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I may be making a rash statement here, but this looks like a job for a semaphore, rather than an event. Create a semaphore with an initial count of 0 and a huge max count (LONG_MAX or something like that). Each time a writer puts something in the queue, it calls ReleaseSemaphore. The reader(s) wait on the semaphore and remove one item from the queue each time they wake up. -------- There are 10 types of people in this world. Those who know binary and those who don't.