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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:47:52+00:00 2026-06-03T01:47:52+00:00

When we create a thread with pthread_create , should we place the pthread_join immediate?

  • 0

When we create a thread with pthread_create, should we place the pthread_join immediate?

For example I have the following two codes, but I do not know why it does not work.

For the 1st version, the output is not deterministic.

#include<iostream>
#include<pthread.h>
#include<cstring>
#include<cstdlib>
#define ROW 3
#define COL 3
using namespace std;
typedef struct {
int row;
int col;
} para;
void print(double * para)
{
    for(int i=0;i<3;i++)
   {
           for(int j=0;j<3;j++)
           {
                    cout<<*(para+3*i+j)<<"\t";
            }
            cout<<endl;
        }

}
double mat[9]={1,2,3,4,5,6,7,8,9};
double * result=(double *) malloc(9*sizeof(double));
void * mul(void * arg)
{
        para * temp=(para *) arg;
        int row=temp->row;
        int col=temp->col;
        double sum=0;
        for(int i=0;i<3;i++)
        {
                double a=*(mat+row*3+i);
                double b=*(mat+i+3*col);
                sum+=a*b;
        }
        *(result+row*3+col)=sum;
int main()
{
        pthread_t thread[9];
        for(int i=0;i<9;i++)
        {
                   para M;
                M.row=i/3;
                M.col=i%3;
                pthread_create(&thread[i],NULL,mul,&M);

        }
        for(int i=0;i<9;i++)
        {
                 pthread_join(thread[i],NULL);            
        }

        print(result);
}

With the 2nd version, the output is correct.

#include<iostream>
#include<pthread.h>
#include<cstring>
#include<cstdlib>
#define ROW 3
#define COL 3
using namespace std;
typedef struct {
int row;
int col;
} para;
void print(double * para)
{
    for(int i=0;i<3;i++)
   {
           for(int j=0;j<3;j++)
           {
                    cout<<*(para+3*i+j)<<"\t";
            }
            cout<<endl;
        }

}
double mat[9]={1,2,3,4,5,6,7,8,9};
double * result=(double *) malloc(9*sizeof(double));
void * mul(void * arg)
{
        para * temp=(para *) arg;
        int row=temp->row;
        int col=temp->col;
        double sum=0;
        for(int i=0;i<3;i++)
        {
                double a=*(mat+row*3+i);
                double b=*(mat+i+3*col);
                sum+=a*b;
        }
        *(result+row*3+col)=sum;
int main()
{
        pthread_t thread[9];
        for(int i=0;i<9;i++)
        {
                   para M;
                M.row=i/3;
                M.col=i%3;
                pthread_create(&thread[i],NULL,mul,&M);
                pthread_join(thread[i],NULL); 
        }   
        print(result);
}

What is the difference between these two usages? And why the first code has something wrong?

  • 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-03T01:47:54+00:00Added an answer on June 3, 2026 at 1:47 am

    The first version starts nine threads.
    Then once all threads have been created it waits for them all to finish before exiting.
    Thus you get nine threads running in parallel.

    The second version starts nine threads.
    But after each thread is started it waits for the thread to exit before continuing.
    Thus you get nine threads running serially.

    Unfortunately the first version is also broken.
    The data object passed to the thread (as the 4th parameter (&M)) is an automatic variable that goes out of scope potentially before the thread completes.

    Fix like this:

        pthread_t thread[9];
        para      M[9];
        for(int i=0;i<9;i++)
        {
                M[i].row = i/3;
                M[i].col = i%3;
                pthread_create(&thread[i],NULL,mul,&M[i]);
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two requests in tomcat. One HTTP request will create a thread. Client
I have used the following code to create two threads: //header files #include <pthread.h>
I use pthread_create to create several child threads. At a time, the main thread
I use pthread_create to create several child threads. At a time, the main thread
If I have two threads (Linux, NPTL), and I have one thread that is
I am trying to create five threads in the main thread. In the pthread_create()
When I create a thread ( pthread_create() ) from my main process, I see
In this homework, two matrices should be added using a different thread for each
The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t
Let's say I: malloc a pthread_t for holding a thread context pthread_create with 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.