简体   繁体   English

为什么我需要从主线程使用 `pthread_exit()`,而它不是由 `pthread_create` 创建的?

[英]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.我对一些我正在测试以开始理解 posix 线程的代码有疑问。

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;非常简单的代码,启动线程并从中打印,这一切都很神奇,除了我不明白return 0;之前的最后一个pthread_exit(NULL) return 0; in the end of the main method.在 main 方法的最后。

It seems that the main thread should not be a pthread, and should not need that!似乎主线程不应该是 pthread,而且应该不需要! 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.如果我不放它,代码就不起作用:代码编译并执行,但我只得到“起始线程”打印消息,而不是“hello from ...”消息。

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> .此外,如果我注释掉 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.如果您不在主函数中使用 pthread_exit,那么所有创建的线程都将在主完成时终止,即在您的情况下,在它们打印任何内容之前。

By calling pthread_exit in the main function makes main wait until all threads have completed.通过在 main 函数中调用 pthread_exit 使 main 等待所有线程完成。

From the pthread_exit man page:从 pthread_exit 手册页:

The process will exit with an exit status of 0 after the last thread has been terminated.在最后一个线程终止后,进程将以退出状态 0 退出。 The behavior is as if the implementation called exit() with a zero argument at thread termination time.行为就像实现调用 exit() 在线程终止时参数为零一样。

This is referring to calling pthread_exit() from the main processes thread.这是指从主进程线程调用 pthread_exit()。

pthread_exit will only terminate the main thread, but will let other threads running until they finish their job. pthread_exit只会终止主线程,但会让其他线程运行直到它们完成它们的工作。 While if you return from main or call exit , this will force killing all threads.而如果您从main返回或调用exit ,这将强制终止所有线程。

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.没有pthread_exit的情况是您已经启动了线程,然后 main 在它实际运行任何线程之前退出。 When main exits the program stops running (as do the threads).当 main 退出时,程序停止运行(线程也是如此)。

Generally you'd use a pthread_join which waits until the specified thread finishes execution.通常,您会使用pthread_join等待指定的线程完成执行。

It seems that pthread_exit causes main to wait for all the threads which I wasn't aware.似乎pthread_exit导致 main 等待我不知道的所有线程。 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.编辑:同样在您的情况下,您根本不需要使用pthread_exit A thread will automatically terminate when the executed function ( printInfo in your case) returns.当执行的函数(在您的情况下为printInfo )返回时,线程将自动终止。 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 . pthread_exit允许您在执行的函数返回之前终止线程,例如,如果执行的函数调用另一个调用pthread_exit函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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