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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:19:53+00:00 2026-05-24T06:19:53+00:00

My program is seemingly having a deadlock problem. Basically I have a class that

  • 0

My program is seemingly having a deadlock problem.

Basically I have a class that looks like this:

class Foo {
public:
  Foo();

  void bar();

private:
  void monitor();

  bool condition_;
  pthread_t monitor_;
  pthread_mutex_t mutex_;
  pthread_cond_t cv_;
};

In Foo‘s constructor, I invoke monitor() in a separate thread (namely monitor_). This monitor() function does the following:

pthread_mutex_lock(&mutex_);
while (true) {
  while (!condition_) {
    pthread_cond_wait(&cv_, &mutex_);
  }
  // do something
  // and then setting condition_ to false
  condition_ = false;
}
pthread_mutex_unlock(&mutex_);

The bar() function is the only public interface (excluding ctor and dtor) of Foo. It also needs to acquire the mutex in its execution. My symptom is that bar() can never acquire mutex_. It looks like pthread_cond_wait() does not release the mutex as it is supposed to do. And if I disable the monitor thread (thus no racing condition), then bar() can run to its completion without any problem.

Of course, the above code is a stripped-down version of my real code. Actually I think there is no logic error in this code and I’m using pthread correctly. I’m suspecting if there are any other causes for this deadlock situation. Can anyone give a clue on this? Thanks!

  • 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-24T06:19:53+00:00Added an answer on May 24, 2026 at 6:19 am

    I would look at your constructor and the bar() function, and possibly if you’ve accidentally made a copy of the object in question. I’ve copied the classes you provided, and my assumptions on how the rest of it operates below. The program below wakes up every second, and signals the thread.

    #include <pthread.h>
    #include <iostream>
    
    class Foo {
    public:
      Foo() {
        condition_ = false;
        pthread_mutex_init(&mutex_, NULL);
        pthread_cond_init(&cv_, NULL);
        pthread_create(&monitor_, NULL,
               startFunc, this);
      }
    
      void bar() {
        pthread_mutex_lock(&mutex_);
        std::cout << "BAR" << std::endl;
        condition_ = true;
        pthread_cond_signal(&cv_);
        pthread_mutex_unlock(&mutex_);
      }
    
    private:
      Foo(const Foo&) {};
      Foo& operator=(const Foo&) { return *this; };
    
      static void* startFunc(void* r) {
        Foo* f = static_cast<Foo*>(r);
        f->monitor();
        return NULL;
      }
    
      void monitor() {
        pthread_mutex_lock(&mutex_);
        while (true) {
          while (!condition_) {
        pthread_cond_wait(&cv_, &mutex_);
          }
          // do something
          // and then setting condition_ to false
          std::cout << "FOO" << std::endl;
          condition_ = false;
        }
        pthread_mutex_unlock(&mutex_);
    
      }
    
      bool condition_;
      pthread_t monitor_;
      pthread_mutex_t mutex_;
      pthread_cond_t cv_;
    };
    
    
    int main() {
      struct timespec tm = {1,0};
      Foo f;
    
      while(true) {
        f.bar();
        nanosleep(&tm, NULL);
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that crashes (seemingly) randomly after 2-4h of runtime. I am
I have a problem that is seemingly solvable by enumerating all possible solutions and
My Program overrides public void paint(Graphics g, int x, int y); in order to
I have an OpenGL program that works on all of my computers but one.
I am having a difficult time with a seemingly easy and embarrassing problem. All
We have a VB.NET program that needs to periodically call a function in an
My program goes through a loop like this: ... while(1){ read(sockfd,buf,sizeof(buf)); ... } The
I am having a hard time with a seemingly simple Azure program. My exercise
Most program languages have some kind of exception handling; some languages have return codes,
My program generates relatively simple PDF documents on request, but I'm having trouble with

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.