Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Thread Synchronization

Tags:

c#

.net

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 download 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.

like image 318
user209293 Avatar asked Mar 08 '10 07:03

user209293


People also ask

What is thread synchronization in C#?

Synchronization is a technique that allows only one thread to access the resource for the particular time. No other thread can interrupt until the assigned thread finishes its task. In multithreading program, threads are allowed to access any resource for the required execution time.

What is .NET synchronization?

Synchronization is a concept that is used to prevent multiple threads from accessing a shared resource concurrently. You can use it to prevent multiple threads from invoking the properties or methods of an object concurrently.

Is .NET multi threaded?

With . NET, you can write applications that perform multiple operations at the same time. Operations with the potential of holding up other operations can execute on separate threads, a process known as multithreading or free threading.

Is C# good for multithreading?

Along with this, C# provides an option to execute the code that can be run in parallel using a multithreading concept, where the process/application can have multiple threads invoked within it.


2 Answers

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.

like image 188
John Knoeller Avatar answered Sep 29 '22 08:09

John Knoeller


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.
like image 44
Jorge Córdoba Avatar answered Sep 29 '22 07:09

Jorge Córdoba