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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:42:53+00:00 2026-06-09T13:42:53+00:00

I am new to these pthreads. I wrote a program so that instead of

  • 0

I am new to these pthreads. I wrote a program so that instead of displaying the numbers randomly, it should display in order. I have used pthrea_join() method for that. The program is as follows:

int cnt=0,i=1;
pthread_t th[10];
int printmsg()
{
  cnt++;
  printf( "thread no. %d\n",cnt);
}
void tosync()
{
  if(i>0)
  pthread_join(th[i-1],NULL); // <---here i am blocking the previous thread..                         
  printmsg();
}
void main(void)
{
  pthread_create(&th[0], NULL,(void*)&tosync, NULL);
  for( i=1;i<10; i++){
    pthread_create(&th[i],NULL, (void*) &tosync, NULL);
  } 
  int y;
  for(int i=0; i<10; i++)
    pthread_join(th[i],NULL);
  return;
}

Still i am getting the numbers randomly…
plzz. help

  • 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-09T13:42:55+00:00Added an answer on June 9, 2026 at 1:42 pm

    pthread_join(th[i-1],NULL) this line have some problems. when you create a thread you increate the value of i. suppose
    create first three thread OS switch thrid thread start and OS switch to the main thread where it creates rest of threads. after create all threads the value of i is 10/
    now suppose OS switch to thrid thread then he waits 10-1 = 9th thread to finish and similary go on.So ultimate it print always randomly. Your strategy is wrong.

    try this

    int cnt=0,i=1;
    pthread_t th[10];
    int printmsg()
    {
    cnt++;
    printf( thread no. %d\n",cnt);
    }
    void tosync()
    {
    
    printmsg();
    }
    void main(void)
    {
    pthread_create(&th[0], NULL,(void*)&tosync, NULL);
    for( i=1;i<10; i++){
    pthread_create(&th[i],NULL, (void*) &tosync, NULL);
    pthread_join(th[i],NULL); // <---here i am blocking the previous thread..   
    } 
    
    return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dijit.form.NumberTextBox input field that starts out with these parms: new dijit.form.NumberTextBox({
I have a ressource manager that handles multiple TCP-Connections. These connections are pthreads. How
arr=Array.new(3) arr[0]=5 arr[1]=3 arr[2]=2 These lines should call this function, https://github.com/ruby/ruby/blob/trunk/array.c#L568 according to this,
I have a lightweight application that catches Xorg and dbus events. In order to
My tools are Linux, gcc and pthreads. When my program calls new/delete from several
OS is Linux, working with pthreads I have two worker threads that run forever,
I have been experimenting with these new 'HTML5:' controls and I'm not sure if
I'm rather new to these could someone explain the significance (of the following code)
I'm kind of new to these things. I do not know what command line
I am trying to figure out exactly what these new fangled data stores such

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.