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 9235047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:56:04+00:00 2026-06-18T06:56:04+00:00

After receiving answers to a previous question on logging on a different thread, I

  • 0

After receiving answers to a previous question on logging on a different thread, I am currently at the following bit of code (note: the concurrent_queue here is from ppl, but any other concurrent_queue should work):

class concurrentFuncQueue
    {
    private:
        typedef std::function<void()> LambdaFunction;
        mutable concurrency::concurrent_queue<LambdaFunction> functionQueue;
        mutable std::atomic<bool> endcond;
        LambdaFunction function;
        std::thread thd;
    public:
        concurrentFuncQueue() : endcond(false), thd([=]{
            while (endcond != true)
            {
                if (functionQueue.try_pop( function ))
                {
                    function(); //note: I am popping a function and adding () to execute it
                }
            }
        }){}
        ~concurrentFuncQueue() { functionQueue.push([=]{ endcond = true; }); thd.join(); }
        void pushFunction(LambdaFunction function) const { functionQueue.push(function); }
    };

Basically the functions I push are run on a different thread sequentially (ex. a logging function) as to avoid performance issues on the main thread.

Current usage is along the following:

static concurrentFuncQueue Logger;
vector<char> outstring(256);
Logger.pushFunction([=]{ OutputDebugString(debugString.c_str()) });

Great so far. I can push functions on to a concurrent queue that will run my functions sequentially on a separate thread.

One thing I also need to have, but currently don’t are return values so that ex (pseudo-code):

int x = y = 3;
auto intReturn = Logger.pushFunction([=]()->int { return x * y; });

will push x * y on to the concurrent queue, and after the pop and completion of the function (on the other thread), returns the value calculated to the caller thread.

(I understand that I’ll be blocking the caller thread until the pushed function is returned. That is exactly what I want)

I get the feeling that I might have to use something along the line of std::promise, but sadly my current low understanding of them prevent me from formulating something codable.

Any ideas? Thoughts on the above C++ code and any other comments are also much welcome (please just ignore the code completely if you feel another implementation is more appropriate or solves the problem).

  • 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-18T06:56:05+00:00Added an answer on June 18, 2026 at 6:56 am

    You should be able to use something along the lines of:

    template<typename Foo>
    std::future<typename std::result_of<Foo()>::type> pushFunction(Foo&& f) {
        using result_type = typename std::result_of<Foo()>::type; // change to typedef if using is not supported
        std::packaged_task<result_type()> t(f);
        auto ret_fut = t.get_future();
        functionQueue.push(std::move(t));
        return ret_fut;
    }
    

    For this to work you need to make your LambdaFunction a type-erased function handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code after modifications, things seems working fine but I am receiving
After my last question was solved the JSON I'm receiving from the server changed
Consider the following code, which takes place in a background thread (thread B): List<T>
Edit: After receiving more information from DCoder, the phrase I was searching for here
In our project we have requirement that, after receiving sms message from third party
I have applications(WinForm) that gets some objects from webservice. After receiving array I transform
Not sure why I am receiving this after the webservice ran and I am
After some time researching and trying different things I still cannot get my @ExceptionHandler
After following the RoR getting started tutorial, I added another model as: $ rails
In my program there is one thread (receiving thread) that is responsible for receiving

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.