这是在一个多线程应用程序以等待变量的最佳方式 [英] What is the best way to wait for a variable in a multithreaded application

查看:78
本文介绍了这是在一个多线程应用程序以等待变量的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对于多线程程序执行类似如下:

  //等待变量成为事实,但不生猪资源
//然后重新同步队列

这样一个很好的解决方案?

 而(!准备好){
    Thread.sleep代码(250); //暂停1/4秒;
};


解决方案

没有,这不是一个很好的解决方案。首先,它可能睡眠时间过长。第二很容易让线程进入步调一致。下面是几个环节MSDN文章在适当的同步技术:

I would like to do something like the below for a multi-threaded program:

// wait for variable to become true but don't hog resources  
// then re-sync queues  

Is something like this a good solution?

while (!ready) {
    Thread.Sleep(250); // pause for 1/4 second;
};

解决方案

No, this is not a good solution. First it might sleep too long. Second it's easy for threads to get into lockstep. Here's couple of links to MSDN articles on proper synchronization techniques:

这篇关于这是在一个多线程应用程序以等待变量的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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