[英]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 技术:
So basically the strategy is: synchronize threads so that所以基本上策略是:同步线程,以便
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.