Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C language and multithread programming

My question is related to thread programming in C.

My problem is that I just want to create two threads in my main program. These two threads should work sequentially, which means my first thread should execute first (no other statement of any thread should be executed). First thread should have complete control. No other statement of any other thread, even main program statements, should be executed until the first thread is complete.

After completion of the first thread, the second thread should be executed in a similar way as the first.

After that my main should execute.

I know you can say why the hell I want to do this, because this thing can be achieved by just creating two functions and calling them in sequence, but for learning and for experimentation I want to do it with the help of threads.

I write some code in C as follows:

void* fun()
{  
    printf("\nThe thread 1 is running");
}
void* van()
{
    printf("\nthread 2 is running ");
}

int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,fun,NULL);
    pthread_create(&t2,NULL,van,NULL);
    printf("\nI'm in main\n");
    pthread_join(t2,NULL); 
}

The program is working perfectly but I don't understanding the working of the function pthread_join().

When I change my code a little bit as follows:

int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,NULL,fun,NULL);
    pthread_join(t2,NULL);  // Change
    pthread_create(&t2,NULL,van,NULL);
    printf("\nI'm in main\n");
}

Now when I run the code it shows a segmentation fault.

Now my questions are the following:

  1. What is the attribute parameter in the pthread_create() function? Why do we use them? What are the default attributes for a thread? Please explain with an example.
  2. What is the argument in pthread_create() function? Why we use them? What are the default arguments for a thread? Please explain with an example.
  3. How does pthread_join() work actually? What does it mean when my code calls pthread_join() in main with t2 as first argument. Does it mean main should suspend its execution until t2 execution has completed or something else?
  4. What is the second argument in pthread_join()? Why do we use it? What is its default value? Please explain with an example or code.
like image 844
Golu Avatar asked Dec 09 '25 10:12

Golu


1 Answers

  1. The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then the thread is created with default attributes (source).

  2. Argument is passed to your thread function. This is the best way of passing data to the thread (as opposed to using global variables, e.g.).

  3. Yes, pthread_join waits until the thread finishes. That's why your program fails when you call pthread_join before you start the thread, since t2 contains junk at that point.

  4. If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the target thread supplied to pthread_exit(3)) into the location pointed to by *retval. If the target thread was canceled, then PTHREAD_CANCELED is placed in *retval. (source).

I.e., you can make your thread function to notify you about its execution result.

Given this, your thread creation may look like this:

struct th_arg{
    ...
};

static void th_work(struct th_arg* a){
     //...some work
    if (success) pthread_exit(EXIT_SUCCESS)
    else pthread_exit(ERROR_CODE);
}

int main(){
     int t1,t2;
     struct th_arg[2];
     int codes[2];
     // initialize th_arg2 
     pthread_create(&t1, NULL, th_work, th_arg+0, th_arg+0);
     pthread_create(&t2, NULL, th_work, th_arg+1, th_arg+1);
     pthread_join(t1, codes+0);
     pthread_join(t2, codes+1);

     if (codes[0] == EXIT_SUCCESS && codes[1] == EXIT_SUCCESS){
             ...
     }
}  
like image 176
elder_george Avatar answered Dec 11 '25 22:12

elder_george



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!