线程,锁定{}行为! [英] Threads, lock{} behaviour!

查看:52
本文介绍了线程,锁定{}行为!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

i我在关闭我的子窗体之前使用锁来终止2个线程。

但是当我关闭窗体时,有时会关闭,有些时候关闭时间它的剂量!



这里是代码:



主线程:

  private   void  Capture_FormClosing(对象发​​件人,FormClosingEventArgs e)
{
lock (lockThread1)
{

quitThread1 = true ;
}
// 等到thread1终止(正常工作)!
// 更好地使用Join而不是thread1.Join();
while (Thread1.ThreadState!= System.Threading.ThreadState.Stopped)
{
Console.WriteLine( 关闭thread1 ...);
// 当关闭失败时,应用程序将在此处进行交易
}
}



帖子1:

  while (条件)
{
// 做一些工作......
lock (lockThread1)
{
if (quitThread1)
return ; // 在线程方法中使用return时,此线程退出其方法并且//其状态更改为已停止
}
}





i对Lock的行为有疑问。

问题:当thread1使用锁时,mainThread等待直到Lock被释放或者它跳过锁定代码?

或者我在代码的另一部分做错了!?



提前Thx。

解决方案

当两个或多个线程试图锁定相同的锁对象时,只有一个可以通过,其他线程被阻塞到等待状态(不是花费任何CPU时间,直到线程被唤醒,通过其他线程释放锁定,中止或超时等条件,并根据队列中的位置唤醒处于等待状态的线程。如果锁定在不同的锁定对象上完成,则此类线程根本不会相互干扰。 主线与否,永远不重要。



我现在回答了你的问题,额外的东西...



请注意,您的代码看起来很可疑,至少在实际编程方面。如果你解释了你的最终目标,我将能够帮助你。同时,请阅读我过去与 System.Windows.Forms 以及之后使用的线程同步相关的答案:

Treeview扫描仪和MD5的问题 [ ^ ],

< a href =http://www.codeproject.com/Answers/159125/Control-Invoke-vs-Control-BeginInvoke#answer1> Control.Invoke()与Control.BeginInvoke() [ ^ ]。br />


-SA


我同意SAK。你的代码确实看起来很可疑。我没有看到Form_Closing中需要Lock,但是再一次,除了你发布的内容之外我对你的代码一无所知。



一个锁应该包裹竞争资源。您不使用它来控制对线程的访问。您可以在线程内部使用它来控制对资源的访问,其中只有一个线程可以访问共享资源。


首先,它似乎不需要完全锁定

布尔 quitThread1 可以原子设置或读取,并且不会出现有两个setter试图设置冲突值的可能性。



如果你真的想要对一个值保证线程安全保护,那么看看互锁类:

http://msdn.microsoft.com/en-us/library/system.threading.interlocked(v = vs.110).aspx [ ^ ]

Hi all,
i am using locks to terminate 2 threads before i close my child Form.
But when i close the form, some times it closes, and some time it dosent!

here is the code:

main-thread:

private void Capture_FormClosing(object sender, FormClosingEventArgs e)
{
    lock (lockThread1)
   {

       quitThread1 = true; 
   }
//wait until thread1 terminates(works fine)!
// Better Use Join instead thread1.Join();
while (Thread1.ThreadState != System.Threading.ThreadState.Stopped)
      {
         Console.WriteLine("Closing thread1...");
         //application is traped in here when closing fail
      }
}


thread 1:

while(condition)
{
//Do some work...
  lock (lockThread1)
  {
      if(quitThread1) 
         return; //when using return in the thread method, this thread quit its method and                   //its state change to stopped
  }
}



i have doubt about the Lock behaviour.
Qestion: when the lock is used by thread1, mainThread wait's until the Lock is released or it skips the in Lock code?
or maybe i am doing somthing wrong in another part of the code!?

Thx in advance.

解决方案

When two or more threads try to get the lock on the same lock object, only one can pass, other threads are blocked to a wait state (which does not spend any CPU time until a thread is waken up, by such condition as release of the lock by other threads, abort or timeout), and the threads in the wait state are waken up, according to their positions in the queue. If the lock is done on different lock objects, such thread do not interfere each other at all. "Main thread" or not, never matters.

I answered your question, now, something extra…

Note that your code looks suspicious, at least in terms of practical programming. If you explain your ultimate goals, I'll be able to help you. In the meanwhile, please read my past answers related to thread synchronization used with System.Windows.Forms and beyond:
Problem with Treeview Scanner And MD5[^],
Control.Invoke() vs. Control.BeginInvoke()[^].

—SA


I agree with SAK. Your code does look suspicious. I don't see the need for the Lock in the Form_Closing, but then again, I know nothing about your code other than what you posted.

A Lock should wrap a "contended resource". You don't use it to control access to a thread. You use it inside threads to control access to a resource where only one thread should have access to a shared resource.


First of all it doesn't appear that you need the lock at all.
The boolean quitThread1 can be set or read atomically, and you don't appear to have a possibility of two setters attempting to set conflicting values.

If you really want to have guaranteed thread safe protection on a value, then look at the Interlocked class:
http://msdn.microsoft.com/en-us/library/system.threading.interlocked(v=vs.110).aspx[^]


这篇关于线程,锁定{}行为!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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