Pthread在创建线程后立即运行 [英] Pthread Run a thread right after it's creation

查看:723
本文介绍了Pthread在创建线程后立即运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用pthread的C程序.

I have a C program in which I use pthread.

我希望新创建的线程在创建后立即运行.

I would like newly created threads to run as soon as they are created.

其背后的原因是我的线程具有用于设置信号处理程序的初始化代码,在主线程发送一些信号之前,我必须确保这些处理程序已准备就绪.

The reason behind this is that my threads have initialisation code to set up signal handlers, and I must be sure the handlers are ready, before my main thread sends some signals.

我已经尝试在pthread_create之后执行pthread_yield,但是没有成功.

I've tried doing pthread_yield just after my pthread_create, but without success.

我怀疑是否会有所作为,但是我在x86_64上运行Linux 3.6.

I doubt it makes a difference, but I am running Linux 3.6 on x86_64.

谢谢

推荐答案

或者您可以使用障碍,即致电此问题.

Or you might use a barrier, i.e. call pthread_barrier_wait (early in the routine of each thread, or at initialization in the main thread), to ensure that every relevant thread has reached the barrier (after which some of your threads could do your naughty signal tricks). See this question.

这篇关于Pthread在创建线程后立即运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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