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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:07:47+00:00 2026-05-26T05:07:47+00:00

I have to make a program which has a shared variable (counter with initial

  • 0

I have to make a program which has a shared variable (counter with initial value = 35) and 5 threads. I have to make the program so that each thread accesses the value of the counter and decrements it by 1. This should continue until counter = 0.

This is what I have for the code so far, but the problem is that only one thread is decrementing the value of counter to 0.

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

#define NTHREADS 5
void *thread_function1(void *);
void *thread_function2(void *);
void *thread_function3(void *);
void *thread_function4(void *);
void *thread_function5(void *);
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
short int  counter = 35;


main()
{
   pthread_t thread_id[NTHREADS];
   pthread_t thread1, thread2, thread3, thread4, thread5;
int status, status2, status3, status4, status5;

status = pthread_create(&thread1, NULL, thread_function1, NULL);
if(status!=0){
    fprintf(stderr,"thread 1 failed\n");    
}

status2 = pthread_create(&thread2, NULL, thread_function2, NULL);
if(status2!=0){
    fprintf(stderr,"thread 2 failed\n");    
}

status3 = pthread_create(&thread3, NULL, thread_function3, NULL);
if(status3!=0){
    fprintf(stderr,"thread 3 failed\n");    
}

status4 = pthread_create(&thread4, NULL, thread_function4, NULL);
if(status4!=0){
    printf("thread 4 failed\n");    
}

status5 = pthread_create(&thread5, NULL, thread_function5, NULL);
if(status5!=0){
    fprintf(stderr,"thread 5 failed\n");    
}

//pthread_join(thread1, NULL);
//int x = counter;

printf("created all the threads \n");

printf("joining thread 1");
    pthread_join(thread1, NULL);
printf("joining thread 2");
    pthread_join(thread2, NULL);
printf("joining thread 3");
    pthread_join(thread3, NULL);
printf("joining thread 4");
    pthread_join(thread4, NULL);
printf("joining thread 5");
    pthread_join(thread5, NULL);                          

   printf("Final counter value: %d\n", counter);
}

void *thread_function1(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());   

while(counter>0){
   srand(time(NULL));

   int r = rand()%3;
  printf(" %d\n", r);
   pthread_mutex_lock( &mutex1 );
  printf("entered the mutex");
   counter--;
   printf(" %d\n", counter);
   sleep(r);
   pthread_mutex_unlock( &mutex1 );
   printf("mutex unlocked");
   pthread_yield();
}

}

void *thread_function2(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());   

while(counter>0){
   srand(time(NULL));

   int r = rand()%3;
  printf(" %d\n", r);
   pthread_mutex_lock( &mutex1 );
  printf("entered the mutex");
   counter--;
   printf(" %d\n", counter);
   sleep(r);
   pthread_mutex_unlock( &mutex1 );
   pthread_yield();
}

}

void *thread_function3(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());   

while(counter>0){
   srand(time(NULL));
   int r = rand()%3;
  printf(" %d\n", r);
   pthread_mutex_lock( &mutex1 );
  printf("entered the mutex");
   counter--;
   printf(" %d\n", counter);
   sleep(r);
   pthread_mutex_unlock( &mutex1 );
   pthread_yield();
}

}

void *thread_function4(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());   

while(counter>0){
   srand(time(NULL));

   int r = rand()%3;
  printf(" %d\n", r);
   pthread_mutex_lock( &mutex1 );
  printf("entered the mutex");
   counter--;
   printf(" %d\n", counter);
   sleep(r);
   pthread_mutex_unlock( &mutex1 );
   pthread_yield();
}

}

void *thread_function5(void *dummyPtr)
{
   printf("Thread number %ld\n", pthread_self());   

while(counter>0){
   srand(time(NULL));

   int r = rand()%3;
   printf(" %d\n", r);
   pthread_mutex_lock( &mutex1 );
   printf("entered the mutex");
   counter--;
   printf(" %d\n", counter);
   sleep(r);
   pthread_mutex_unlock( &mutex1 );
   pthread_yield();
}

}

Can anyone please help ?
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-26T05:07:48+00:00Added an answer on May 26, 2026 at 5:07 am
    int r = rand()%3;
    /* ... */
    sleep(rand);
    

    r is a random number between 0 and 2, but you sleep rand seconds, which is the address of a function, which will be implicitly casted to an unsigned int – so the thread will sleep for a very very long time. Use sleep(r); instead.

    Also, note that you read counter while not holding the mutex (in while(counter > 0)), which may lead to the program working incorrectly depending on architecture, caching and compiler optimizations. You should lock the mutex, read the value of counter to a local variable, then unlock the mutex and check whether the value is positive.

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

Sidebar

Related Questions

I have a little program that I want to make open automatically when my
I have a class which has a constructor that takes a const char* .
So I have been given a C program, which I am trying to make
I have the main component of my program which runs in the main thread.
I'm writing a simple plugin based program. I have an interface IPlugin which has
I have a program which gets commands as a string. Each character in the
I have a program which has slow computations and I wish to debug the
I have made a control which has a property that is calculated by using
I have an old ClickOnce program, which has been replaced by a new one
I have a few questions related: 1) Is possible to make my program change

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.