Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. WaitForSingleObject

WaitForSingleObject

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdata-structures
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    act_x
    wrote on last edited by
    #1

    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 ?

    J 1 Reply Last reply
    0
    • A act_x

      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 ?

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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, create AddedtoQueue_A as automatic (see the docs for CreateEvent) and do not use ResetEvent. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      A 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        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, create AddedtoQueue_A as automatic (see the docs for CreateEvent) and do not use ResetEvent. Good luck. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

        A Offline
        A Offline
        act_x
        wrote on last edited by
        #3

        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

        J 1 Reply Last reply
        0
        • A act_x

          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

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          OK, this kind of stuff is always somewhat messy. Try this. Define a CRITICAL_SECTION, say CS_A (don't forget to initialize it with InitializeCriticalSection), create AddedtoQueue_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

          J 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            OK, this kind of stuff is always somewhat messy. Try this. Define a CRITICAL_SECTION, say CS_A (don't forget to initialize it with InitializeCriticalSection), create AddedtoQueue_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

            J Offline
            J Offline
            JT Anderson
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups