Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:04:31+00:00 2026-05-31T02:04:31+00:00

I have a background thread: class Queue { public: Message* WaitForMessage(uint32_t uTimeOutMS); private: SignalObject

  • 0

I have a background thread:

class Queue
{
public:
  Message* WaitForMessage(uint32_t uTimeOutMS);

private:
  SignalObject soMessage; // Signalled when a message is added to the queue
};

class MyThread : Thread
{
public:
  bool IsToStop() const { return soStop.IsSignalled(); }

  void ThreadFunction()
  {
     while (!IsToStop()) {
        Message* pMessage = queue1.GetMessage();
        if (pMessage != nullptr) ProcessMessage(pMessage);

        pMessage = queue2.GetMessage();
        if (pMessage != nullptr) ProcessMessage(pMessage);

        Sleep(100); etal // If I wait for 1 ms I hog the CPU, if I wait for 100 ms I waste time when the thread should be stopped
     };
  }

private:
  SignalObject soStop; // Signalled when the thread should stop

  Queue queue1;
  Queue queue2;
};

I would like to get rid of the Sleep and just wait on the signal objects at the same time (This should free up some CPU while I wait for the signal objects).
I could make them the signal objects on the Queue public and then do something like this:

while (true) {
  WaitForSignalObjectsForever(soStop, queue1.soMessage, queue2.soMessage); // Returns after any signal object is signalled
  if (IsToStop()) break;

  Message* pMessage = queue1.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);

  pMessage = queue2.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);
}

or this:

while (true) {
  soStop.WaitForSignalObjectsForever(queue1.soMessage, queue2.soMessage); // Returns after any signal object is signalled
  if (IsToStop()) break;

  Message* pMessage = queue1.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);

  pMessage = queue2.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);
}

or this:

while (WaitForSignalObjectsOrIsToStopForever(queue2.soMessage, queue1.soMessage)) { // Returns true if soStop is signalled, false if one of the other signal objects was signalled
  Message* pMessage = queue1.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);

  pMessage = queue2.GetMessage();
  if (pMessage != nullptr) ProcessMessage(pMessage);
}

I think I prefer the first version because it is more generic. I still feel icky about making the signal objects public (Or protected with friend access) though. Does any one have a better solution?

Also I am used to calling them signal objects, is there a more common name for them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:04:32+00:00Added an answer on May 31, 2026 at 2:04 am

    in windows you have WaitForMultipleObjects, you could write something like this ( in pseudo code )

    while (!gotallmessages())
         WaitForMultipleObjects( {queue1.signal, queue2.signal} )
    

    another solution would be to create several threads for monitoring the queue’s:

    void queuemonitor(master*m, Queue *q)
    {
         msg= q->GetMessage();
         m->notify();
    }
    

    and in the master thread do something like this: ( using condition + mutex from boost/thread.hpp )

    condition cond;
    mutex  mtx;
    
    void notify(int id)
    {
         scoped_lock l(mtx);
         notifycount++;
         cond.notify_one();
    }
    
    void masterthread()
    {
         scoped_lock l(mtx);
         while (notifycount<2)
             cond.wait(l);
    
         // ... this is what we waited for
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Communicator that works in a background thread receiving data on
I'm trying to have a class that when created, starts a background thread, similar
Folks, Here is a simplified code for my background thread: public class MyThread extends
I have a C# class library which starts up a background consumer thread (lazily)
I have a background thread that handles communication with an external service. Each time
I have a background thread running that fires events, but how can I ensure
I have a background thread and the thread calls some methods that update the
Hi all i have a background worker thread and some unmanaged code dlls ,
I have a timely operation that runs on a background thread. While running, I
I have data that needs to be executed on a certain background thread. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.