在C程序多线程 [英] Multiple threads in C program

查看:115
本文介绍了在C程序多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C语言编写它采用多线程控制Unix的应用程序。我在与主要功能终端问题之前就已经催生了线程有一个变化来完成他们的工作。我如何prevent这种情况的发生。我怀疑我需要使用在pthread_join简陋,但我不知道怎么样。谢谢!

I'm writing a Unix application in C which uses multiple threads of control. I'm having a problem with the main function terminating before the thread it has spawned have a change to finish their work. How do I prevent this from happening. I suspect I need to use the pthread_join primitive, but I'm not sure how. Thanks!

推荐答案

是的,你可以使用在pthread_join()(看其他anwers对如何做到这一点)。但让我解释一下pthread的模型,告诉你另一种选择。

Yes, you could use pthread_join() (see other anwers for how to do that). But let me explain the pthread model and show you another option.

在Unix下,一个进程退出时从主主线程返回,当任何线程调用exit()或当最后一个线程调用了pthread_exit()。基于最后一个选项,你可以简单地拥有你的主线程调用了pthread_exit()和至少一个多线程在运行过程中会留只要活着。

In Unix, a process exits when the primary thread returns from main, when any thread calls exit() or when the last thread calls pthread_exit(). Based on the last option, you can simply have your main thread call pthread_exit() and the process will stay alive as long as at least one more thread is running.

这篇关于在C程序多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆