Linux等待线程全部停止的方法 (linux等待所有线程结束)

在Linux系统中,线程是可以进行并发操作的,多个线程可以同时运行,因此在编写多线程程序时,需要考虑线程的同步与状态控制。在多线程程序中,有时需要等待所有线程完成任务后再进行下一步操作,这就需要使用一些方法来实现线程的等待。

在本文中,将介绍几种Linux系统中等待线程停止的方法。

1.使用pthread_join函数

pthread_join函数用于等待指定的线程终止并回收其资源。当调用pthread_join函数时,主线程会被阻塞,直到指定的线程终止为止。

这个函数的调用格式如下所示:

“`

int pthread_join(pthread_t thread, void **retval);

“`

其中,参数thread是要等待的线程ID,参数retval是线程的返回值。如果不需要返回值,可以将retval设置为NULL。

在调用pthread_join函数时,如果指定的线程没有终止,则主线程会一直等待,直到被指定的线程终止。

使用pthread_join函数等待线程停止的示例代码如下:

“`c

#include

#include

#include

#include

void *thread_func(void *arg)

{

printf(“Sub thread is running\n”);

sleep(3);

printf(“Sub thread is exiting\n”);

pthread_exit(NULL);

}

int mn()

{

pthread_t tid;

int ret;

void *retval;

ret = pthread_create(&tid, NULL, thread_func, NULL);

if (ret != 0)

{

printf(“Create thread fled\n”);

return -1;

}

pthread_join(tid, &retval);

printf(“Mn thread is exiting\n”);

return 0;

}

“`

在这个示例中,主线程创建了一个子线程,子线程运行了3秒后就退出了。主线程调用pthread_join函数等待子线程终止,然后输出“Mn thread is exiting”并退出。

2.使用pthread_cond_wt函数配合pthread_cond_signal函数

pthread_cond_wt和pthread_cond_signal函数用于线程之间的条件等待和条件唤醒。使用条件变量可以实现线程的同步与等待。

pthread_cond_wt函数的原型如下所示:

“`c

int pthread_cond_wt(pthread_cond_t *cond, pthread_mutex_t *mutex);

“`

参数cond是条件变量,参数mutex是互斥锁。在调用pthread_cond_wt函数前,线程必须先获取到mutex的锁,然后才能调用pthread_cond_wt函数等待条件变量。

当条件变量被唤醒时,pthread_cond_wt函数会自动释放mutex锁,然后再返回,线程可以从等待状态中恢复。

pthread_cond_signal函数的原型如下所示:

“`c

int pthread_cond_signal(pthread_cond_t *cond);

“`

参数cond是条件变量,调用pthread_cond_signal函数后,等待在该条件变量上的一个线程会被唤醒。

pthread_cond_wt和pthread_cond_signal函数需要搭配使用,使用示例代码如下:

“`c

#include

#include

#include

#include

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int done = 0;

void *thread_func(void *arg)

{

printf(“Sub thread is running\n”);

sleep(3);

printf(“Sub thread is exiting\n”);

pthread_mutex_lock(&mutex);

done = 1;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);

}

int mn()

{

pthread_t tid;

int ret;

void *retval;

ret = pthread_create(&tid, NULL, thread_func, NULL);

if (ret != 0)

{

printf(“Create thread fled\n”);

return -1;

}

pthread_mutex_lock(&mutex);

while (!done)

{

pthread_cond_wt(&cond, &mutex);

}

pthread_mutex_unlock(&mutex);

printf(“Mn thread is exiting\n”);

return 0;

}

“`

在这个示例中,主线程创建了一个子线程,子线程运行了3秒后就退出了。主线程使用互斥锁和条件变量等待子线程终止。当done变量的值被设置为1时,子线程会调用pthread_cond_signal函数唤醒一个等待在条件变量上的线程,主线程就可以从等待状态中恢复。

3.使用pthread_barrier_wt函数

pthread_barrier_wt函数用于等待一个屏障,屏障可以控制多个线程同时开始或结束。当等到所有线程到达屏障时,pthread_barrier_wt函数会返回,并允许所有线程同时开始或结束。

pthread_barrier_wt函数的原型如下所示:

“`c

int pthread_barrier_wt(pthread_barrier_t *barrier);

“`

参数barrier是屏障变量,在调用pthread_barrier_wt函数前,所有线程必须先调用pthread_barrier_init函数初始化屏障变量。

在多线程程序中使用pthread_barrier_wt函数等待线程停止的示例代码如下:

“`c

#include

#include

#include

#include

pthread_barrier_t barrier;

void *thread_func(void *arg)

{

printf(“Sub thread is running\n”);

sleep(3);

printf(“Sub thread is exiting\n”);

pthread_barrier_wt(&barrier);

pthread_exit(NULL);

}

int mn()

{

pthread_t tid1, tid2;

int ret;

void *retval;

pthread_barrier_init(&barrier, NULL, 2);

ret = pthread_create(&tid1, NULL, thread_func, NULL);

if (ret != 0)

{

printf(“Create thread1 fled\n”);

return -1;

}

ret = pthread_create(&tid2, NULL, thread_func, NULL);

if (ret != 0)

{

printf(“Create thread2 fled\n”);

return -1;

}

pthread_barrier_wt(&barrier);

printf(“Mn thread is exiting\n”);

return 0;

}

“`

在这个示例中,主线程创建了两个子线程,当所有子线程到达屏障时,主线程就可以从等待状态中恢复,并输出“Mn thread is exiting”并退出。在使用pthread_barrier_wt函数时,必须先调用pthread_barrier_init函数初始化屏障变量。

相关问题拓展阅读:

linux下多进程或者多线程编程的问题。新手,望指教!

之一个问题,不管是创建进程或者创建线程都不会阻塞,创建完毕马上返回不会等待子进程或者子线程的运行

第二个问题

首先进程和线程是不一样的

多进程时,父进程如果先结束,那么子进程会被init进程接收成为init进程的子进程,接下来子进程接着运行,直到结束,init进程负责轿晌取得这些子进程的结束状态并释放进程资源。而如果是子进程先结束,那么父进程应当用wait或者waitpid去获取子进程的结束状态并释放进程资源,否则子进程会成为僵死进程,它占用的闭陪锋进程资源不会释放

多线程时,如果父线程或者说你讲的main结束时使用return或者exit或者处理完毕结束,那么整个进程都结束,其他子线程自然结束。如果main结束时使乱神用的是pthread_exit那么只有父线程结束,子线程还在运行。同样对于子线程结束时如果调用了exit,那么整个进程包括父线程结束,如果调用了pthread_exit或者正常结束,那么只有子线程结束。

另外子线程结束时如果没有分离属性,其他线程应当使用pthread_join去获取线程结束状态并释放线程资源,如同进程里的wait和waitpid

你好,多进程或多线御御程,都不会阻塞当前语句代码。为了您的理解,我就大胆举下面两个例子:

多进程:你可以看成是本来是一条路的,现在从中间拆成两条,然后每一条路都有属于自己这条路的代码在运行。

多线程:你可以看成是一条路,然后分出车道,比如档拆锋左车道和右车道甚至是停车道,然后每条车道都单独通车,其他车道的不能对这条车道进行干扰。

所以,把一条路从中间行晌拆成两条,成本是很高的。但是把一条路分车道,成本就不是很高了。

对于您提出的main函数的疑问,当main函数最后执行完毕,程序退出后,所有的进程包括线程,都会被关闭的,哪怕你的程序中没有关闭,操作系统也会帮你关闭的,现在的操作系统都非常的完善了。当然,也存在有线程或进程不被释放的特殊情况,更好在编程中要记得释放。

关于linux等待所有线程结束的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » Linux等待线程全部停止的方法 (linux等待所有线程结束)