1

I'm trying to understand how multithreading works. I've written the following code `

void handler(void *arg)
{
    printf("Printf from cleanup handler: %s\n", (char*)arg);
}

void print(const char *msg)
{
    printf("%7s: Pid=%d Tid:%lu\n", msg, getpid(), pthread_self());
}

void* thread_function1(void *args)
{
    printf("Received: %d\n", (int)args);
    print("Thread");
    pthread_cleanup_push(handler, "hello");
    pthread_cleanup_pop(1);
    printf("Thread Done\n");    
    return (void *) 0;
}

int main(void)
{
    pthread_t tid1, tid2;
    void *tret;
    if(pthread_create(&tid1, NULL, thread_function1, (void *)1))
        exit(1);
    if(pthread_create(&tid2, NULL, thread_function1, (void *)2))
        exit(1);
//  pthread_join(tid2, &tret);
//  pthread_join(tid1, &tret);
}

The problem with this code is that the main completes its execution before thread_function1 could complete its execution. If both the comments are removed then thread 2 is executed only after thread 1 has completeted its execution.

What I want to do is have thread 1 and thread 2 execute simultaneously and main should wait for completion of both the threads.

2
  • 2
    Do the threads do a (non-trivial) amount of work? Could it be that thread_function1 finishes before the second thread is even created? Commented Nov 6, 2016 at 11:29
  • @WanderNauta I've updated the question. Please take a look at it. Commented Nov 6, 2016 at 11:54

1 Answer 1

5

The problem with this code is that the main completes its execution before thread_function1 could complete its execution.

That's because when the main thread exits, the process dies including all threads. You can instead call pthread_exit(0) from the main thread so that the rest of the threads continue execution and the main thread exits.

If both the comments are removed then thread 2 is executed only after thread 1 has completeted its execution.

That's not true. i.e. tid1 and tid2 are executed simultaneously after they are created ("simultaneous execution" depends on your hardware, scheduling policy etc -- but as far as you program is concerned, they can be considered as being executed simultaneously). pthread_join() doesn't control the order of threads' execution. It only affects the order in which the main thread waits for the completion of the threads i.e. main thread waits for tid2 to complete first and then waits for tid1 (if you ncomment those two lines).

Sign up to request clarification or add additional context in comments.

5 Comments

I get the output as "Received: 1 \n Printf from cleanup handler: hello \n Received: 2 \n Printf from cleanup handler: hello" This means thread 2 was executed only after thread 1 was completed. Am I missing something?
The order is not fixed. Try executing it repeatedly a few times and you'll probably see "Received 2" first and then "Received 1".
Thank you so much. I tried running it multiple times and it works exactly as you mentioned. Thank you ! :)
I also another problem in your updated code: thread_function1 doesn't have a declaration when you first use it. Either move it above the main function. Or, declare a prototype for it before main(): void *thread_function1(void*);. Good luck!
This is not the real code. I've added the code just to show what I'm doing. I'll edit the question. Thanks though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.