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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:02:41+00:00 2026-05-26T11:02:41+00:00

I intent to have two threads waiting for signals from a third thread. These

  • 0

I intent to have two threads waiting for signals from a third thread.

These two threads do the same work but only one of them gets the signal at a time. Once a certain condition is meet(number of signal captured), they terminate themselves.

Then in the end the main thread cancel the third thread.

I got deadlock, but could not figure out where is the problem.

#include <pthread.h>
#include <stdio.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int n = 0;
int count = 0;

void* func1(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func1\n", n);
            return (NULL);
        }
        else
        {
            printf("func1 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}

void* func2(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func2\n", n);
            return (NULL);
        }
        else
        {
            printf("func2 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}    

void* func3(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        n++;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
}


int main(int argc, char *argv[])
{
    pthread_t t1, t2, t3;

    pthread_create(&t1, NULL, func1, NULL);
    pthread_create(&t2, NULL, func2, NULL);
    pthread_create(&t3, NULL, func3, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    pthread_cancel(t3);

    return 0;
}
  • 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-26T11:02:41+00:00Added an answer on May 26, 2026 at 11:02 am

    You’re not unlocking the mutex when func1 or func2 exits.

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

Sidebar

Related Questions

I have two intents that I would like Users to choose from. One intent
I have two activities. The first one executes the second one. Intent i =
I have two activities with exactly same intent filters and I would like to
I have a intent receiver in my android manifest, but would like to give
I have activity A in package one, and I want to run an intent
I have two simple activities A and B . The user starts B from
I have created two projects. The first one, extracts information of handset and shows
I created one simple list view. i have two list view, my 1st list
I have two Live Wallpapers that belong to the same Application and I am
I have two activities: one displays an image and a button, the other displays

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.