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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:15:02+00:00 2026-06-16T03:15:02+00:00

What I want to accomplish: In the main function are created two threads. They

  • 0

What I want to accomplish:

In the main function are created two threads. They increment a global variable with the number 5. And send a signal to consumer thread that decrements the variables.In the consumer thread between each decrementation the current value is displayed. The main thread has to wait until all the threads are finished and then exit.

What I get:

Some times the main function exits before the consumer had a chance to display the results. I’m using pthread_join, but it returns error code 3.

Any Ideas how to get the wanted results?

The code is bellow.

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

static pthread_mutex_t mtx;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void *producer(void *arg);
void *consumer(void *arg);
static int avail = 0;

int main(int argc, char *argv[]){

    pthread_t cons1, prod1, prod2;

    int status;
    int t1;
    int t2;
    int t3;

    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    pthread_mutexattr_t mtxAttr;
    pthread_mutexattr_settype(&mtxAttr, PTHREAD_MUTEX_ERRORCHECK);
    pthread_mutex_init(&mtx, &mtxAttr);

    t1 = pthread_create(&prod1, &attr, producer, NULL);
    if(t1 != 0){
    perror("problem1");
    }
    t2 = pthread_create(&prod2, &attr, producer, NULL);
    if(t2 != 0){
    perror("problem2");
    }
    t3 = pthread_create(&cons1, &attr, consumer, NULL);
    if(t3 != 0){
    perror("problem3");
    }

    status = pthread_join(t1, NULL);
    if(status != 0){
    perror("can't join1");
    }

    status = pthread_join(t2, NULL);
    if(status != 0){
    perror("can't join2");
    printf("\n%d\n", status);
    }
    status = pthread_join(t3, NULL);
    if(status != 0){
    printf("%s",strerror(errno));
    }
    printf("\nend result \t%d\n",avail);
    printf("fin\n");
    //while(1){}
    return 0;
}

void *producer(void *arg){
    int s;
    printf("producer\n");
    s = pthread_mutex_lock(&mtx);
    avail+=5;
    s = pthread_mutex_unlock(&mtx);
    s = pthread_cond_signal(&cond);
    pthread_exit(NULL);
}

void *consumer(void *arg){
    int s;
    while(1) {
    s = pthread_mutex_lock(&mtx);
    if(s !=0 ){
        perror("lock err");
    }
    while (avail == 0) {
        s = pthread_cond_wait(&cond, &mtx);
    }
    while (avail > 0) {
        avail--;
        printf("Temp: %d \n",avail);
    }

    s = pthread_mutex_unlock(&mtx);
    }
    printf("done");
    pthread_exit(NULL);
}
  • 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-16T03:15:03+00:00Added an answer on June 16, 2026 at 3:15 am

    Don’t join on t1, t2 or t3. Those are the return codes of the pthread_create() function. Use pthread_join() on prod1, prod2 and cons1 instead. And please compile with -Wall -Wextra.

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

Sidebar

Related Questions

If I have two files helper.app and main.app, I want to be able to
I have a problem with the function replace . What I want to accomplish
I want to accomplish this (mysql) query: UPDATE table SET field = field +
I want to accomplish a few tasks: Open a Word document through my program
I want to accomplish something like the following (my interest is in the toInt()
I want to accomplish nearly the same thing as this question , which is
What I want to accomplish is the effect I have seen in this site
What I want to accomplish: [a, b, c, d] -> [ (a, x), (b,
This question does pretty much what I want to accomplish, but my table is
Maybe I should first give an idea of what I want to accomplish as

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.