简体   繁体   English

如何在没有 pthread_join() 的情况下使 pthreads 并发工作

[英]How to make pthreads work concurrent without pthread_join()

I'm making a program where I have multiple threads working at the same time.我正在制作一个程序,其中有多个线程同时工作。 After messing with the code for a while, I had an issue where I would send a string ( char * ) to the thread to do further operations with, and somehow the string did not send at all.在弄乱了代码一段时间后,我遇到了一个问题,我将向线程发送一个字符串 ( char * ) 以进行进一步的操作,但不知何故该字符串根本没有发送。

Later, I wrote a very simple code where I just send a string to a thread and the function prints it to the console.后来,我写了一个非常简单的代码,我只是将一个字符串发送到一个线程,然后 function 将它打印到控制台。 I found out that without using pthread_join() , it wouldn't work at all.我发现如果不使用pthread_join() ,它根本不起作用。 I have no idea why, because I know that whenever pthread_join() is called, it blocks every other thread and waits until that one is finished.我不知道为什么,因为我知道每当调用pthread_join()时,它都会阻塞所有其他线程并等待该线程完成。

Here's the simple program:这是一个简单的程序:

void* prisntstr(void* string);

int main(int argc, char *argv[])
{
    char* string = "Hello!";

    pthread_t thread;

    pthread_create(&thread, NULL, prisntstr, (void*)string);

    pthread_join(thread, NULL);

}

void* prisntstr(void* string)
{
    char* str = (char*)string;

    printf("%s\n", str);
}

Does anyone know how I can do this without using pthread_join() ?有谁知道我如何在不使用pthread_join()的情况下做到这一点?

The problem with your program, if you remove the pthread_join , is that main is returning and thereby causing the program to exit without doing anything to synchronize with the other thread and determine whether it's actually finished.如果您删除pthread_join ,您的程序的问题是main正在返回,从而导致程序退出而没有做任何与其他线程同步并确定它是否实际完成的事情。 When main returns, it's just as if you called exit , and the whole process terminates.main返回时,就像您调用了exit一样,整个过程终止。

Aside from pthread_join there are lots of ways you could make main wait for other actions to be finished.除了pthread_join之外,还有很多方法可以让main等待其他操作完成。 You could wait on a semaphore that the other threads post, you could loop on a condition variable inspecting data other threads set, etc.您可以等待其他线程发布的信号量,您可以循环检查其他线程设置的数据的条件变量等。

Alternatively you could have main call pthread_exit so to terminate the initial thread.或者,您可以main调用pthread_exit以终止初始线程。 Then the process will not exit until you call exit or until each thread has exited.然后在您调用exit或每个线程退出之前,该进程不会退出。

The thread calling pthread_join() just waits for another thread to finish.调用pthread_join()的线程只是等待另一个线程完成。 Please note that:请注意:

If that thread has already terminated, then pthread_join() returns immediately如果该线程已经终止,则pthread_join()立即返回

It is the most elegant way for doing it, being the others more complicated, and involving IPC tecniques:这是最优雅的方式,其他方式更复杂,并且涉及 IPC 技术:

  • the calling thread could wait for a mutex/semaphore put by the secondary thread调用线程可以等待辅助线程放置的互斥体/信号量
  • the calling thread could wait for a signal sent by the secondary thread调用线程可以等待辅助线程发送的信号
  • ... and so on ... 等等

So basically the strategy is: synchronize threads so that所以基本上策略是:同步线程,以便

  1. The main thread can obtain some information calculated by the child thread主线程可以获取子线程计算出来的一些信息
  2. The process is kept alive until the child thread has completed its action (like in this case)该进程一直保持活动状态,直到子线程完成其操作(如本例中)

The reason why without pthread_join() you dont see that message printed to stdout is that as soon as main terminate, it terminates the whole process and all children threads are terminated ;没有pthread_join()您看不到打印到 stdout 的消息的原因是,只要 main 终止,它就会终止整个进程并且所有子线程都将终止 in your case before the print is executed.在您的情况下,在执行打印之前。

  • When you use thread in your program, created thread will became child thread and the main program will became main thread.当您在程序中使用线程时,创建的线程将成为子线程,主程序将成为主线程。 So, we need to block the main thread so that it can not close.所以,我们需要阻塞主线程,使其无法关闭。

  • If main thread closed then child threads will be exit as well.如果主线程关闭,则子线程也将退出。 So, You need to find the way how to block main thread to not exit.因此,您需要找到阻止主线程不退出的方法。 thread gives us the facility for that is pthread_join().线程为我们提供了 pthread_join() 的功能。 This will block the main thread until the child thread is working.这将阻塞主线程,直到子线程工作。 Main thread will block on pthread_join() line.主线程将阻塞 pthread_join() 行。 It will not execute other lines.它不会执行其他行。

  • If you want to not use pthread_join() you need to find other ways for that.如果你不想使用 pthread_join() 你需要找到其他方法。

Ex:

while(1);
  • while(1);而(1); will not end the main thread until you kill the process.在您终止进程之前不会结束主线程。

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

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