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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:12:16+00:00 2026-06-05T08:12:16+00:00

The problem is in following: I want to write a short program that creates

  • 0

The problem is in following:

I want to write a short program that creates 10 threads and each prints a tread “id” that is passed to thread function by pointer.

Full code of the program is below:

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

struct params {
        pthread_mutex_t mutex;
        int id;
};

typedef struct params params_t;

void* hello(void* arg){
    int id;
    pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
    id = (*(params_t*)(arg)).id;
    pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
    printf("Hello from %d\n", id);
}


int main() {
    pthread_t threads[10];
    params_t params;
    pthread_mutex_init (&params.mutex , NULL);

    int i;
    for(i = 0; i < 10; i++) {
            params.id = i;
            if(pthread_create(&threads[i], NULL, hello, &params));
    }

    for(i = 0; i < 10; i++) {
            pthread_join(threads[i], NULL);
    }

    return 0;
}

The supposed output is (not necessary in this order):

Hello from 0
....
Hello from 9

Actual result is:

Hello from 2
Hello from 3
Hello from 3
Hello from 4
Hello from 5
Hello from 6
Hello from 8
Hello from 9
Hello from 9
Hello from 9

I tried to place mutex in different places in hello() function, but it didn’t help.

How should I implement thread sync?

EDIT: Supposed result is not necessary 0…9 it can be any combination of these numbers, but each one should appear only one time.

  • 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-05T08:12:17+00:00Added an answer on June 5, 2026 at 8:12 am

    There are two problems:

    A. You’re using a lock but main is unaware of this lock.

    B. A lock is not enough in this case. What you would want is for threads to cooperate by signalling each other (because you want main to not increment the variable until a thread says that it is done printing it). You can use a pthread_cond_t to achieve this (Look here to learn more about this). This boils down to the following code (basically, I added an appropriate usage of pthread_cond_t to your code, and a bunch of comments explaining what is going on):

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct params {
            pthread_mutex_t mutex;
            pthread_cond_t done;
            int id;
    };
    
    typedef struct params params_t;
    
    void* hello(void* arg){
    
        int id;
        /* Lock.  */
        pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
    
        /* Work.  */
        id = (*(params_t*)(arg)).id;
        printf("Hello from %d\n", id);
    
        /* Unlock and signal completion.  */
        pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
        pthread_cond_signal (&(*(params_t*)(arg)).done);
    
        /* After signalling `main`, the thread could actually
        go on to do more work in parallel.  */
    }
    
    
    int main() {
    
        pthread_t threads[10];
        params_t params;
        pthread_mutex_init (&params.mutex , NULL);
        pthread_cond_init (&params.done, NULL);
    
        /* Obtain a lock on the parameter.  */
        pthread_mutex_lock (&params.mutex);
    
        int i;
        for(i = 0; i < 10; i++) {
    
                /* Change the parameter (I own it).  */    
                params.id = i;
    
                /* Spawn a thread.  */
                pthread_create(&threads[i], NULL, hello, &params);
    
                /* Give up the lock, wait till thread is 'done',
                then reacquire the lock.  */
                pthread_cond_wait (&params.done, &params.mutex);
        }
    
        for(i = 0; i < 10; i++) {
                pthread_join(threads[i], NULL);
        }
    
        /* Destroy all synchronization primitives.  */    
        pthread_mutex_destroy (&params.mutex);
        pthread_cond_destroy (&params.done);
    
        return 0;
    }
    

    I see that the example you are trying is a toy program to probably learn about the POSIX thread library. In the real world, as we all know this can be done much faster without even using threads. But you already know this.

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

Sidebar

Related Questions

Following problem: I want to render a news stream of short messages based on
I have the following problem. I want to check that the item clicked on
I have the following problem. I want to write my own slide show in
I'm trying to learn Haskell and want to write a small program which prints
I have the following problem. I want to write a method with which i
I'm trying to write a trigger that deals with the following problem . I'm
The problem is following: I want to automate the way my emacs starts. It
Im facing the following problem: I want to integrate the mercurial hash into my
I have the following problem: Within a stylesheet document I want to create an
Hi I have the following problem, I want to laod a website with php.

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.