向休眠线程发出信号
我是 C# 中多线程的新手,对所有线程的内容感到困惑。 这就是我想要的:
public class TheClass
{
Thread _thread;
bool _isQuitting;
bool _isFinished;
object jobData;
public void Start()
{
jobData = new object();
_thread = new Thread(new ThreadStart(Run));
_thread.Start();
}
private void Run()
{
while (!_isQuitting)
{
lock (jobData) // or should i use mutex or monitor?
{
DoStuff();
}
// sleep till get triggered
}
DoCleanupStuff();
_isFinished = true;
}
public void AddJob(object param)
{
jobData = param;
//wake up the thread
}
public void Stop()
{
_isQuitting = true;
//wake up & kill the thread
//wait until _isFinished is true
//dispose the _thread object
}
}
在本课程中,实现注释掉的短语和整体性能的最有效方法是什么?
I'm new to multithreading in c# and am confused among all thread stuff.
here is what I'm trying to have:
public class TheClass
{
Thread _thread;
bool _isQuitting;
bool _isFinished;
object jobData;
public void Start()
{
jobData = new object();
_thread = new Thread(new ThreadStart(Run));
_thread.Start();
}
private void Run()
{
while (!_isQuitting)
{
lock (jobData) // or should i use mutex or monitor?
{
DoStuff();
}
// sleep till get triggered
}
DoCleanupStuff();
_isFinished = true;
}
public void AddJob(object param)
{
jobData = param;
//wake up the thread
}
public void Stop()
{
_isQuitting = true;
//wake up & kill the thread
//wait until _isFinished is true
//dispose the _thread object
}
}
in this class, what is the most efficient method to achieve commented out phrases and for overall performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑使用
Monitor.Wait()
和
监视器。脉冲()
。例如:
但是,请注意,在我的代码中,您仍然只有一个
jobData
对象 - 结果是AddJob()
将等待当前作业完成处理。这可能不是您想要的。也许您应该有一个作业数据对象的Queue
;AddJob
会将一个项目入队
,而Run
将保持出队
直到队列为空。如果这样做,Run
中的锁定语句应仅包含对队列的访问,而不是整个处理阶段。Consider using
Monitor.Wait()
andMonitor.Pulse()
.For example:
However, notice that in my code you still have only one
jobData
object — the consequence being thatAddJob()
will wait for the current job to finish processing. This may not be what you want. Perhaps you should have aQueue<T>
of job data objects;AddJob
wouldEnqueue
an item andRun
would keepDequeue
’ing until the queue is empty. If you do this, the lock statement inRun
should encompass only the access to the queue, not the entire processing stage.我不知道性能,但看起来你想要 自动重置事件。
这无疑为您提供了执行您所描述的操作的最简单方法之一。对其进行测试,如果您发现性能是一个问题,那么就考虑另一种方法来实现它。
I don't know about the performance, but it looks like you want AutoResetEvent.
This certainly gives you one of the simplest ways to do what you're describing. Test it out, and if you find performance is an issue, then worry about another way to do it.
在我看来,您想使用生产者/消费者模式。使用阻塞队列最容易实现这一点。一旦你有了这个,那么其他一切就很容易了。您可以在此处找到实现,或者您可以使用BlockingCollection 类在 4.0 中可用。
注意:为简洁起见,省略了强化代码。
It looks to me like you want to use the producer/consumer pattern. This is most easily accomplished using a blocking queue. Once you have that then everything else is easy. You can find an implementation here or you can use the BlockingCollection class available in 4.0.
Note: Hardening code is omitted for brevity.