多线程程序可以确定吗? [英] Can a multi-threaded program ever be deterministic?

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

问题描述

通常,多线程程序是不确定的,这意味着,如果多线程程序崩溃,则几乎不可能重新创建导致该情况的错误.谁真的不知道接下来要运行什么线程,以及什么时候该线程会被再次抢占.

Normally it is said that multi threaded programs are non-deterministic, meaning that if it crashes it will be next to impossible to recreate the error that caused the condition. One doesn't ever really know what thread is going to run next, and when it will be preempted again.

当然,这与OS线程调度算法有关,而且这一事实不知道下一步将运行哪个线程以及该线程将有效运行多长时间. 程序执行顺序也起作用,等等.

Of course this has to do with the OS thread scheduling algorithm and the fact that one doesn't know what thread is going to be run next, and how long it will effectively run. Program execution order also plays a role as well, etc...

但是,如果您拥有用于线程调度的算法,又又知道何时运行什么线程,那么多线程程序又可以变成确定性"呢?例如,您将能够重现崩溃?

But what if you had the algorithm used for thread scheduling and what if you could know when what thread is running, could a multi threaded program then become "deterministic", as in, you'll be able to reproduce a crash?

推荐答案

了解该算法实际上无法让您预测何时会发生什么.程序或线程执行中发生的各种延迟取决于环境条件(可用RAM->交换,其他繁忙任务,传入的中断等).

Knowing the algorithm will not actually allow you to predict what will happen when. All kinds of delays that happen in the execution of a program or thread are dependent on environmental conditions (free RAM -> swapping, other busy tasks, interrupts coming in, etc.).

如果将多线程程序映射到顺序执行,并且线程本身是确定性的,则整个程序可能是确定性的,并且并发"问题可以重现.当然,到那时,它们将不再是并发问题.

If you were to map your multi-threaded program to a sequential execution, and your threads in themselves are deterministic in their behaviour, then your whole program could be deterministic and 'concurrency' issues could be made reproducible. Of course, at that point they would not be concurrency issues any more.

如果您想了解更多信息,请 http://en.wikipedia.org/wiki/Process_calculus 非常有趣.

If you would like to learn more, http://en.wikipedia.org/wiki/Process_calculus is very interesting reading.

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

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