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

  • Home
  • SEARCH
  • 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 8465551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:04:58+00:00 2026-06-10T15:04:58+00:00

In my C++ application, I have 2 threads: (i) main thread, (ii) background thread.

  • 0

In my C++ application, I have 2 threads: (i) main thread, (ii) background thread.
I have a class defined as:

class helper
{
 public:
     bool login(const string& username, const string& password);
     void logout();

 private:
     bool loginInternal(const string& username, const string& password);
     void logoutInternal();
}

helper::login() and helper::logout() functions (and several other member functions with various return types and # of params and param types) are called on the main thread. In the implementations of these functions, the corresponding internal functions are to be enqueued in a queue, and the background thread calls these internal functions in the order in which they were enqueued. So something like this:

bool helper::login(const string& username, const string& password)
{
    queue.push_back(boost::bind(&helper::loginInternal, this, username, password));
}

void helper::logout()
{
    queue.push_back(boost::bind(&helper::logoutInternal, this));
}

All this time the background thread is running, waiting for the queue to fill up, and as soon as it does, this background thread would start calling the functions in the queue:

queue.front()();
queue.pop_front();

So the question is, how do I define such a queue?

deque<???> queue;

What could be the data type for this queue such that it can hold callback functions with different signatures in the same queue?

EDIT:
Here’s the solution (thanks to J. Calleja):

typedef boost::function<void ()> Command;
deque<Command> queue;

and then call the functor like this:

// Execute the command at the front
Command cmd = queue.front();
cmd();

// remove the executed command from the queue
queue.pop_front();
  • 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-06-10T15:05:00+00:00Added an answer on June 10, 2026 at 3:05 pm

    You may use boost::function if you normalize the return type or ignore it. This library defines a wrapper that can store any element that matches your signature (function or functor).

    Using your example:

    #include <boost/function.hpp>
    
    class helper 
    {  
    public:      
      bool login(const std::string& username, const std::string& password);
      void logout();
    private:      
      bool loginInternal(const std::string& username, const std::string& password);
      void logoutInternal();
    private:
      typedef boost::function<void ()> Command;
      std::deque<Command> queue;
    };
    

    This example ignores the return type as it declares the functions returning void. If you want to know the return value, you have to make logout return a bool and change the declaration to:

      typedef boost::function<bool ()> Command;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
In my application in C++ I am using pthreads. I have a main thread
I have a class to launch background operations in a WinForms application. I need
I have app that uses two threads: one thread for regular application work and
Hi I have an application which after login, loads a main activity which contains
Windows Forms, .net 2.0 My main application thread has a form (A). I have
Suppose I have a GUI application which has a background thread which runs for
I have a background thread that is sending data about the application's current status
I'm working on an image processing application where I have two threads on top
We are observing 4-6 threads on Windows 7 x64 in the application which have

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.