简体   繁体   中英

Why do I need to use `pthread_exit()` from the main thread, while it was not created by a `pthread_create`?

I have a question about some code I'm testing to start understanding posix threads.

I have this basic code:

#include <iostream>
#include <string>
#include <sstream>
#include <pthread.h>

using namespace std;

void *printInfo(void *thid){
    long tid;
    tid =(long)thid;
    printf("Hello from thread %ld.\n",tid);
    pthread_exit(NULL);
}

int main (int argc, char const *argv[])
{
    int num =8;
    pthread_t threadlist[num];
    int rc;
    long t;
    for(t=0;t<num;t++){
        printf("Starting thread %ld\n",t);
        rc = pthread_create(&threadlist[t],NULL,printInfo,(void *)t);
        if(rc)
        {
            printf("Error creating thread");
            exit(-1);
            
        }
    }
    pthread_exit(NULL);
    return 0;
}

Very simple code, start threads and print from them, this all works wonders, except that I don't understand the last pthread_exit(NULL) before the return 0; in the end of the main method.

It seems that the main thread should not be a pthread, and should not need that! If I don't put it, the code does not work: the code compiles and executes, but I only get the "starting thread" print messages, and not the "hello from ..." messages.

So basically I want to know why is that needed.

Also, the code compiles and executes properly if I comment out the include <psthread.h> .

If you don't use pthread_exit in the main function then all created threads are terminated as main finishes, ie in your case before they have printed anything.

By calling pthread_exit in the main function makes main wait until all threads have completed.

From the pthread_exit man page:

The process will exit with an exit status of 0 after the last thread has been terminated. The behavior is as if the implementation called exit() with a zero argument at thread termination time.

This is referring to calling pthread_exit() from the main processes thread.

pthread_exit will only terminate the main thread, but will let other threads running until they finish their job. While if you return from main or call exit , this will force killing all threads.

You need to join your threads. What's happening without the pthread_exit is that you have started the threads, and then main exits before it has actually run any of the threads. When main exits the program stops running (as do the threads).

Generally you'd use a pthread_join which waits until the specified thread finishes execution.

It seems that pthread_exit causes main to wait for all the threads which I wasn't aware. I'd look into that however, as it's not in the documentation (as far as I've read anyway) and may not be something you want to rely on.

Edit: also in your case you don't need to use pthread_exit at all. A thread will automatically terminate when the executed function ( printInfo in your case) returns. pthread_exit allows you to terminate a thread before the executed function returns, for example, if the executed function calls another function which calls pthread_exit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM