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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:01:46+00:00 2026-05-27T14:01:46+00:00

I understand I’ve asked this question before: What is the C++ equivalent for AutoResetEvent

  • 0

I understand I’ve asked this question before: What is the C++ equivalent for AutoResetEvent under Linux?

However, I’m learning that in C++0x, the threading library are made much simpler, so I want to pose this question out again, is there an easy way to implement AutoResetEvent in C++0x?

  • 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-27T14:01:47+00:00Added an answer on May 27, 2026 at 2:01 pm

    Here is a translation of the accepted answer to your first question to use C++11 tools:

    #include <mutex>
    #include <condition_variable>
    #include <thread>
    #include <stdio.h>
    
    class AutoResetEvent
    {
      public:
      explicit AutoResetEvent(bool initial = false);
    
      void Set();
      void Reset();
    
      bool WaitOne();
    
      private:
      AutoResetEvent(const AutoResetEvent&);
      AutoResetEvent& operator=(const AutoResetEvent&); // non-copyable
      bool flag_;
      std::mutex protect_;
      std::condition_variable signal_;
    };
    
    AutoResetEvent::AutoResetEvent(bool initial)
    : flag_(initial)
    {
    }
    
    void AutoResetEvent::Set()
    {
      std::lock_guard<std::mutex> _(protect_);
      flag_ = true;
      signal_.notify_one();
    }
    
    void AutoResetEvent::Reset()
    {
      std::lock_guard<std::mutex> _(protect_);
      flag_ = false;
    }
    
    bool AutoResetEvent::WaitOne()
    {
      std::unique_lock<std::mutex> lk(protect_);
      while( !flag_ ) // prevent spurious wakeups from doing harm
        signal_.wait(lk);
      flag_ = false; // waiting resets the flag
      return true;
    }
    
    
    AutoResetEvent event;
    
    void otherthread()
    {
      event.WaitOne();
      printf("Hello from other thread!\n");
    }
    
    
    int main()
    {
      std::thread h(otherthread);
      printf("Hello from the first thread\n");
      event.Set();
    
      h.join();
    }
    

    Output:

    Hello from the first thread
    Hello from other thread!
    

    Update

    In the comments below tobsen notes that AutoResetEvent has the semantics of signal_.notify_all() instead of signal_.notify_one(). I haven’t changed the code because the accepted answer to the first question used pthread_cond_signal as opposed to pthread_cond_broadcast and I am leading with the statement that this is a faithful translation of that answer.

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

Sidebar

Related Questions

I understand this might be a very basic question. Windows Azure needs me to
I understand that this is a reference for the caller object. I'm used to
I understand that System.Security.Cryptography has a MD5 hashing method in MD5.ComputeHash . However, the
I understand that salts make the same password hash to different values. However, salts
I understand that according to this issue ticket on google code http://code.google.com/p/fullcalendar/issues/detail?id=143 that there
Understand that 1x1 widget size should be 72 dip x 72 dip, but this
I understand that some countries have laws regarding website accessibility. In general, what are
I understand that there are several ways to blend XNA and WPF within the
I understand that they are both supposed to be small, but what are the
I understand that there is a *.className selector since there can be multiple html

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.