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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:22:51+00:00 2026-06-14T03:22:51+00:00

So, I’m writing a program for class that runs 4 threads concurrently. I’ve got

  • 0

So, I’m writing a program for class that runs 4 threads concurrently. I’ve got the program working perfectly, except for the fact that it’s stopping during run-time. I’m not sure if it’s related to the way I have my pthread_cond_wait’s set up, or if its something else. I’ve traced through the program by hand multiple times, and can’t find an explanation.
Here’s my code:

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

//#define TENP .1
//#define FIFTP .15

void *tenPercentA();
void *tenPercentB();
void *fiftPercentC();
void *fiftPercentD();

pthread_cond_t aPirate;
pthread_cond_t bPirate;
pthread_cond_t cPirate;
pthread_cond_t dPirate;
pthread_mutex_t mutex;
int pearls = 1000;

int main()
{
        pthread_t tid;

        pthread_setconcurrency(4);
        pthread_create(&tid, NULL, (void *(*)(void *))tenPercentA, NULL);
        pthread_create(&tid, NULL, (void *(*)(void *))tenPercentB, NULL);
        pthread_create(&tid, NULL, (void *(*)(void *))fiftPercentC, NULL);
        pthread_create(&tid, NULL, (void *(*)(void *))fiftPercentD, NULL);

        pthread_exit(0);
}

void *tenPercentA(){
        int totalA = 0;
        double tempA = 0;

        while(pearls > 0){
                pthread_mutex_lock(&mutex);
                if(pearls > 0){
                        tempA = pearls * .1;
                        tempA = ceil(tempA);
                        totalA = totalA + tempA;
                        pearls = pearls - tempA;
                        printf("Pirate A stole %1.1f pearls.\n", tempA);
                        printf("Pirate A's total: %d\n", totalA);
                        sleep(1);
                        pthread_cond_broadcast (&bPirate);
                        pthread_cond_broadcast (&cPirate);
                        pthread_cond_broadcast (&dPirate);
                        tempA = 0.0;
                }
                else{
                        printf("No more pearls!\n");
                        exit(0);
                }
                pthread_mutex_unlock(&mutex);
                pthread_cond_wait (&aPirate, &mutex);
        }
}

void *tenPercentB(){
        int totalB = 0;
        double tempB = 0;

        while(pearls > 0){
                pthread_mutex_lock(&mutex);
                if(pearls > 0){
                        tempB = pearls * .1;
                        tempB = ceil(tempB);
                        totalB = totalB + tempB;
                        pearls = pearls - tempB;
                        printf("Pirate B stole %1.1f pearls.\n", tempB);
                        printf("Pirate B's total: %d\n", totalB);
                        sleep(1);
                        pthread_cond_broadcast (&aPirate);
                        pthread_cond_broadcast (&cPirate);
                        pthread_cond_broadcast (&dPirate);
                        tempB = 0.0;
                }
                else{
                        printf("No more pearls!\n");
                        exit(0);
                }
                pthread_mutex_unlock(&mutex);
                pthread_cond_wait (&bPirate, &mutex);
        }
}


void *fiftPercentC(){
        int totalC = 0;
        double tempC = 0;

        while(pearls > 0){
                pthread_mutex_lock(&mutex);
                if(pearls > 0){
                        tempC = pearls * .15;
                        tempC = ceil(tempC);
                        totalC = totalC + tempC;
                        pearls = pearls - tempC;
                        printf("Pirate C stole %1.1f pearls.\n", tempC);
                        printf("Pirate C's total: %d\n", totalC);
                        sleep(1);
                        pthread_cond_broadcast (&bPirate);
                        pthread_cond_broadcast (&aPirate);
                        pthread_cond_broadcast (&dPirate);
                        tempC = 0.0;
                }
                else{
                        printf("No more pearls!\n");
                        exit(0);
                }
                pthread_mutex_unlock(&mutex);
                pthread_cond_wait (&cPirate, &mutex);
        }

}

void *fiftPercentD(){
        int totalD = 0;
        double tempD = 0;

        while(pearls > 0){
                pthread_mutex_lock(&mutex);
                if(pearls > 0){
                        tempD = pearls * .15;
                        tempD = ceil(tempD);
                        totalD = totalD + tempD;
                        pearls = pearls - tempD;
                        printf("Pirate D stole %1.1f pearls.\n", tempD);
                        printf("Pirate D's total: %d\n", totalD);
                        sleep(1);
                        pthread_cond_broadcast (&bPirate);
                        pthread_cond_broadcast (&cPirate);
                        pthread_cond_broadcast (&aPirate);
                        tempD = 0.0;
                }
                else{
                        printf("No more pearls!\n");
                        exit(0);
                }
                pthread_mutex_unlock(&mutex);
                pthread_cond_wait (&dPirate, &mutex);
        }

}

And here’s some sample output that it does during run-time:

Pirate A stole 100.0 pearls.
Pirate A's total: 100
Pirate B stole 90.0 pearls.
Pirate B's total: 90
Pirate C stole 122.0 pearls.
Pirate C's total: 122

The only idea I have that might explain it is that the program is getting stuck when the CPU scheduler has two threads ready to run back to back. For example (in the schedule queue): A | B | C | C | D. Any suggestions?

  • 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-14T03:22:52+00:00Added an answer on June 14, 2026 at 3:22 am
     while(pearls > 0){
         pthread_mutex_lock(&mutex);
         if(pearls > 0){
              <do some stuff>
    
         } else {
               print "all done";
         }                
         pthread_mutex_unlock(&mutex);
         pthread_cond_wait (&bPirate, &mutex);
     }
    

    So if all the pirates wait at pthread_condition_wait then when the last pirate steals the last pearls you might find that pearls == 0 and exit the loop. Now you’ll never print your all done condition. And you’ll exit without anything being printed by falling off the end of the program.

    That’s why your program is stopping and not printing anything.

    Edit: (the above is still true, but isn’t the problem you are seeing at the moment)
    From http://linux.die.net/man/3/pthread_cond_wait:

    They shall be called with mutex locked by the calling thread or undefined behavior results

    These functions atomically release mutex

    Upon successful return, the mutex shall have been locked and shall be owned by the calling thread

    So what you want is:
    pthread_mutex_lock(&mutex);
    while (pearls > 0) {
        <do your thing>
        <signal other threads>
        pthread_cond_wait(&mutex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

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.