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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:59:16+00:00 2026-06-15T14:59:16+00:00

I want to implement a mechanism that allows me to block program flow until

  • 0

I want to implement a mechanism that allows me to block program flow until an async operation has completed. (Mostly to be used in unit tests where there is no message loop.)

The code I have creates a thread and waits for a condition notification inside the thread:

#include <chrono>
#include <condition_variable>
#include <iostream>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <thread>

struct Blocker {
    Blocker() :
        wait_thread([this]() {
            std::mutex mtx;
            std::unique_lock<std::mutex> lck(mtx);            
            cond.wait(lck);
        })
    {
    }

    void wait() { wait_thread.join(); }

    void notify() { cond.notify_one(); }

    std::condition_variable cond;    
    std::thread wait_thread;
};

template<typename Callback>
void async_operation(const Callback & cb) { cb(); }

int main() {
    Blocker b;
    async_operation([&](){ b.notify(); });
    b.wait();
}

The problem is that it often deadlocks because the call to notify occurs before the thread even started. How should I fix this?

  • 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-15T14:59:18+00:00Added an answer on June 15, 2026 at 2:59 pm
    #include <mutex>
    #include <condition_variable>
    
    struct blocker
    {
      blocker () : done (false) {}
    
      void
      notify ()
      {
        std::unique_lock<std::mutex> lock (m);
        done = true;
        c.notify_all (); 
      }
    
      void
      wait ()
      {
        std::unique_lock<std::mutex> lock (m);
        while (!done)
          c.wait (lock);
      }
    
      bool done;
      std::mutex m;
      std::condition_variable c;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I am planning to implement a table checksum mechanism to validate that replication has
I want to implement an intialization mechanism that is annotation-based in Java. Specifically, I
I want to implement an in-app purchasing mechanism that supports both Google checkout and
I want to implement Find mechanism (like text editors or word) in my JTextPane.
i want to implement a dynamic relation mechanism with python something like: a:=10 b:=30
I am working on a project that requires that i implement a mechanism for
I want to implement a sort of preview mechanism for my application. I have
I want to implement a floating information pop-up (or how to call it), that
I'm trying to get a general smooth scrolling mechanism that I can implement in

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.