为什么我的Perl线程在第一次运行时随机执行,但在随后的运行中却顺序执行? [英] Why do my Perl threads execute randomly on the first run but in order on subsequent runs?

查看:212
本文介绍了为什么我的Perl线程在第一次运行时随机执行,但在随后的运行中却顺序执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在测试问题代码的过程中如何在Perl中存储两次调用之间的每个线程状态?我第一次执行脚本时,线程执行之间就相互交错了.但是在脚本的所有后续执行中,所有线程几乎都按照创建的顺序完美地运行,并且几乎没有交织.这是Ubunutu 9.04上的Perl ithreads.

In the course of testing the code for the question How can I store per-thread state between calls in Perl? I noticed that the first I time execute the script the threads execution are fairly well interleaved with each other. But on all subsequent executions of the script all the threads run almost perfectly in the order of their creation with very little interleaving. This is Perl ithreads on Ubunutu 9.04.

也许有人可以启发我发生了什么事?

Maybe someone could enlighten me about what's going on?

推荐答案

您的线程按创建顺序运行主要是由于Perl和操作系统中的实现细节(主要是因为它们的单独执行时间低于操作系统的最短执行时间,时间片).要交织线程,可以使用sleep而不是yield(或使线程做一些实际工作).

Your threads are running in creation order largely due to implementation details in Perl and the operating system (mainly because their individual execution time is below the operating system's shortest execution-time slice). To interleave the threads, you can use sleep rather than yield (or make the threads do some real work).

请记住,Perl线程中的yield只是一个建议,这与yield在某些其他语言中的工作方式不同.由于Perl线程是并发的,并且很大程度上由操作系统的调度程序管理,因此除非您使用某种互斥体来阻止执行,否则实际上不可能预测它们的执行顺序.

Keep in mind that yield in Perl threads is just a suggestion, which is unlike the way yield works in some other languages. Since Perl threads are concurrent and are largely managed by the operating system's scheduler, unless you use some sort of mutex to block execution, its not really possible to predict their execution order.

这篇关于为什么我的Perl线程在第一次运行时随机执行,但在随后的运行中却顺序执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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