pthread_create是否启动创建的线程? [英] Does pthread_create start the created thread?

查看:667
本文介绍了pthread_create是否启动创建的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数"pthread_create" 是启动线程(开始执行其功能)还是只是创建它并使其等待正确的时间启动?

Does the function "pthread_create" start the thread (starts executing its function), or does it just creates it and make it wait for the right moment to start?

推荐答案

pthread_create创建线程(通过内部使用clone syscall),并返回tid(线程ID,如pid).因此,在返回pthread_create时,至少会创建一个新线程.但是启动时没有任何保证.

pthread_create creates the thread (by using clone syscall internally), and return the tid (thread id, like pid). So, at the time when pthread_create returns, the new thread is at least created. But there are no guaranties when it will be started.

来自男人: http://man7.org/linux/man-pages/man3/pthread_create .3.html

除非实时调度策略 正在被使用,在调用pthread_create()之后,它是 确定哪个线程(调用者线程或新线程)下一步 执行.

Unless real-time scheduling policies are being employed, after a call to pthread_create(), it is indeterminate which thread—the caller or the new thread—will next execute.

POSIX在pthread_create http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_create.html

在实现上,没有要求在新创建的线程开始执行之前可用已创建线程的ID.

There is no requirement on the implementation that the ID of the created thread be available before the newly created thread starts executing.

还有一个很长的理由",为什么pthread_create是没有单独的线程创建和start_execution的单步过程(就像在旧的Java时代那样):

There is also long "Rationale" why pthread_create is single step process without separate thread creation and start_execution (as it was in good old Java epoch):

pthread_create()的建议替代方法是定义两个单独的操作:create和start.一些应用程序会发现这种行为更为自然.特别是Ada,将任务的创建"与任务的激活"分开.

A suggested alternative to pthread_create() would be to define two separate operations: create and start. Some applications would find such behavior more natural. Ada, in particular, separates the "creation" of a task from its "activation".

拆分操作被标准开发人员拒绝的原因有很多:

Splitting the operation was rejected by the standard developers for many reasons:

  • 启动线程所需的调用次数将从一增加到两个,因此给不需要额外同步的应用程序带来了额外负担.但是,可以通过增加启动状态属性的复杂性来避免第二次调用.

  • The number of calls required to start a thread would increase from one to two and thus place an additional burden on applications that do not require the additional synchronization. The second call, however, could be avoided by the additional complication of a start-up state attribute.

将引入一个额外的状态:已创建但未开始".这将要求标准在目标尚未开始执行时指定线程操作的行为.

An extra state would be introduced: "created but not started". This would require the standard to specify the behavior of the thread operations when the target has not yet started executing.

对于那些需要这种行为的应用程序,可以使用当前提供的功能来模拟两个单独的步骤.通过等待启动操作发出的条件变量,start_routine()可以进行同步.

For those applications that require such behavior, it is possible to simulate the two separate steps with the facilities that are currently provided. The start_routine() can synchronize by waiting on a condition variable that is signaled by the start operation.

您可以使用RT计划;或者只是在创建的线程中添加一些同步,以获取有关其执行的确切信息.在某些情况下,使用 pthread_setaffinity_np

You may use RT scheduling; or just add some synchronization in the created thread to get exact information about it's execution. It can be also useful in some cases to manually bind the thread to specific CPU core using pthread_setaffinity_np

这篇关于pthread_create是否启动创建的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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