开发者

.NET Thread Synchronization

开发者 https://www.devze.com 2022-12-22 17:55 出处:网络
I am planning to use Auto reset Event Handle for Inter Thread communication. EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset);

I am planning to use Auto reset Event Handle for Inter Thread communication.

EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset); 

My producer thread code look like below

produceSomething(); 
handle.Set();

In the consumer thread, I have to do开发者_JAVA技巧wnload data for every one minute or when producer is called Set method

try 
{ 
    while(true) 
    { 
        handle.WaitOne(60000, false); 
        doSomething();  // Downloads data from Internet.
                        // Takes lot of time to complete it.
    } 
} 
catch(ThreadAbortException) 
{ 
    cleanup(); 
} 

My question is if consumer thread is running doSomething function and producer calls set function, what would be state of Auto reset event object?

My requirement is as soon as producer calls set method I have to download fresh data from the Internet. If doSomething function is running, when Producer calls set method, I have to interrupt it and call again.


An auto-reset event is like a gate that closes after the first thread goes through. If you set it while one or more threads are waiting then One thread wakes up, then the event is reset, the rest of the threads continue to wait.

If you set when no threads are waiting, then the first thread that calls handle.WaitOne will not wait, but it will cause the event to get reset and then continue on.

from http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx

Calling Set signals AutoResetEvent to release a waiting thread. AutoResetEvent remains signaled until a single waiting thread is released, and then automatically returns to the non-signaled state. If no threads are waiting, the state remains signaled indefinitely.

If a thread calls WaitOne while the AutoResetEvent is in the signaled state, the thread does not block. The AutoResetEvent releases the thread immediately and returns to the non-signaled state.


The problem with auto-reset event in your scenario is that "setting" it do not supports queuing.

That is, setting an auto-reset event allows one thread to enter, if you set it again before any thread "consumes" your event, then that "set" will be lost. You might expect for two threads to be able to enter and consume whatever you have produced but in fact only ONE thread will be able to do that.

In your case, if you're producing at a faster rate than you're consuming then the auto-reset event might be missleading. Imagine this case.

  • The producer produces one item.
  • The consumer consumes the item (resets the event and starts downloading from inet)
  • The producer produces a second item.
  • The producer produces a third item.
  • The produce stops.
  • The consumer consumes the second item (resests the event and starts downloading again)
  • The consumer WON'T cosume the third item ever because the autoreset event has been reset.
0

精彩评论

暂无评论...
验证码 换一张
取 消