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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:23:06+00:00 2026-06-12T17:23:06+00:00

hi there i’m working on POSIX threads and i have a work like We

  • 0

hi there i’m working on POSIX threads and i have a work like

  • We have 5 threads which each of them will print ‘a’,’b’,’c’,’d,’e’ separately with using same function
  • Threads must be created in alphabetical order.
  • Each character must be printed 50 times on screen
  • ‘a’ can only printed after all ‘c’s are printed
  • ‘b’ can only printed after all ‘d’s are printed
  • ‘c’ can only printed after all ‘d’s are printed

so before i display my code i have to explain what i have done. first of all, i have 5 global variables (which are flags and all of them set to 1 initiallly ) and i assigned them to the value which pthread_create() function returns. and then in the main function i used if() conditions before i joined threads. but i guess i’m trying the wrong way. the issue is i coundnt handle with the process “before thread_a, thread_d must finish its work”. i will appreciate if you can help and suggest some better ideas. and here is my code,

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

int counter = 0;

int flag_a = 1;
int flag_b = 1;
int flag_c = 1;
int flag_d = 1;
int flag_e = 1;

pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;

void* thread_function(void* str);
void print_char(char* ch);

int main(){

    pthread_t thread_a, thread_b, thread_c, thread_d, thread_e;

    char* a = "a";
    char* b = "b";
    char* c = "c";
    char* d = "d";
    char* e = "e";


    flag_a = pthread_create( &thread_a, NULL, thread_function, (void*) a );
    flag_b = pthread_create( &thread_b, NULL, thread_function, (void*) b );
    flag_c = pthread_create( &thread_c, NULL, thread_function, (void*) c );
    flag_d = pthread_create( &thread_d, NULL, thread_function, (void*) d );
    flag_e = pthread_create( &thread_e, NULL, thread_function, (void*) e );

    if(flag_c==0)
        pthread_join( thread_a, NULL );

    pthread_join( thread_b, NULL );

    if(flag_d==0)
        pthread_join( thread_c, NULL );

    if(flag_d==0)
        pthread_join( thread_d, NULL );

    pthread_join( thread_e, NULL );

    printf("\nCounter = %d\n", counter);

    return 0;
}

void* thread_function(void* str) {

    int i;
    char* msg = (char*) str;

        for(i=0; i<50; i++) {

            pthread_mutex_lock(&mymutex);
            counter++;  
            print_char(msg);
            pthread_mutex_unlock(&mymutex);

        }
    }

void print_char(char* ch){

    int i;

    for(i=0; i<1; i++) {
        printf("%s --> ", ch);
        fflush(stdout);
        sleep(1);
    }
}

edit: i found a solution which prints the characters properly but the issue i cant print them asynchronousnly. i mean whenever i run the code it has same output like:

e–>d–>c–>b–>a (every character printed at 50 times )

and this the editted part of thread_function()

void* thread_function(void* str) {

    int i;
    char* msg = (char*) str;

        while( (strcmp(msg,"a") == 0) && (counter_c < 5) ){
        ;;
    }

        while( (strcmp(msg,"c") == 0) && (counter_d < 5) ){
        ;;
    }

        while( (strcmp(msg,"b") == 0) && (counter_d < 5) ){
        ;;
    }

        for(i=0; i<5; i++) {

            pthread_mutex_lock(&mymutex);               
            counter++;

                if( strcmp(msg,"d") == 0 )
                    counter_d++;

                if( strcmp(msg,"c") == 0 )
                    counter_c++;

            print_char(msg);
            pthread_mutex_unlock(&mymutex);

        }
    }
  • 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-12T17:23:07+00:00Added an answer on June 12, 2026 at 5:23 pm

    Logically, you have a dependency graph, and you need a way for each thread to a) wait for some condition to happen before it can begin to print it’s stuff, and b) signal that it has completed printing, so that other threads waiting can start. In pthreads, this can be done with condition variables. So what you could do, instead of passing a pointer to the character to print, pass a pointer to a struct containing the character to print, a condition variable (and associated mutex) to wait on before starting the printing, and another condition variable with associated mutex to signal when done. And then in the main thread, before creating the threads, you of course need to setup all these structs appropriately, and after creating the threads, the main threads should signal those condition variables which are at the end (or beginning, depending on how you look at it..) of the dependency chain(s) in order to get the whole process started.

    For in introduction how to use condition variables, see e.g. https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables

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

Sidebar

Related Questions

There are several shading languages available today like GLSL, HLSL, CG, which one to
There will be 500+ threads concurrently uploading an unique object to a bucket all
There is a component that I have been using since IE7 and had never
There's a webpage with something annoying on it which I'd like to hide every
There is a properly formatted text in Html textarea which looks like : As
There are numerous Agile software development methods. Which ones have you used in practice
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
There is this strange situation I'm fighting on. I have 3 pages, les call

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.