Thread safety questions

F

Frank Rizzo

I tried it on dotnet.vb, but got no answers. I'll try it here now.

I have an app that listen to data on a certain resource. Data can come
in on this resource any time, so I have to listen for it the entire
time. I wanted to see if what I have is thread-safe and makes sense.
Consider the code below. The app starts and creates code to handle an
even from the Listener class. The listener class creates a new thread
(will call it thread BOB) inside it that listens to data. When the data
arrives, it is received on the BOB thread. Then an event is fired, which
is then handled in the calling class (Form1 in this case). That is
followed by a lengthy database operation. My questions are as follows:

1.When event is fired, and Form1.HandleData function kicks in, is it
received on the main application thread or the new BOB thread? If
HandleData runs on the main thread, where, when and how is the event
marshalled between threads?
2.When I run my lengthy database operation in HandleData, what is
blocked? The main application thread or the new BOB thread? Also, will I
be able to still receive data in my BOB thread, when the lengthy
database operation is performed?
3.Is this type of code safe?
4.Is it good practice? If not, what are the alternatives?

using System.Threading;
public class Listener
{
public delegate void DataArrivedEventHandler();
private DataArrivedEventHandler DataArrivedEvent;

public static event DataArrivedEventHandler DataArrived
{
add {Delegate.Combine(DataArrivedEvent, value);}
remove {Delegate.Remove(DataArrivedEvent, value);}
}

private static Thread oThread;
public static void Listen ()
{
oThread = new Thread(new System.Threading.ThreadStart(
StartListening));
oThread.Name = "BOB";
oThread.Start();
}

private static void StartListening ()
{
do {
//blocking call
if (IsDataAvailable())
DataArrivedEvent ();
} while (true);
}
}

public class CallerClass
{
//The Application starts here.
public void Main ()
{
Thread.CurrentThread.Name = "MAIN";
Listener.DataArrived += new
Listener.DataArrivedEventHandler(HandleData);
}

private void HandleData ()
{
//lengthy database operation
}
}
 
J

Jon Skeet [C# MVP]

Frank Rizzo said:
I tried it on dotnet.vb, but got no answers. I'll try it here now.

I have an app that listen to data on a certain resource. Data can come
in on this resource any time, so I have to listen for it the entire
time. I wanted to see if what I have is thread-safe and makes sense.
Consider the code below. The app starts and creates code to handle an
even from the Listener class. The listener class creates a new thread
(will call it thread BOB) inside it that listens to data. When the data
arrives, it is received on the BOB thread. Then an event is fired, which
is then handled in the calling class (Form1 in this case). That is
followed by a lengthy database operation. My questions are as follows:

1.When event is fired, and Form1.HandleData function kicks in, is it
received on the main application thread or the new BOB thread? If
HandleData runs on the main thread, where, when and how is the event
marshalled between threads?

It's in the new thread. Calling a delegate (in the normal way) doesn't
do anything magical thread-wise.
2.When I run my lengthy database operation in HandleData, what is
blocked? The main application thread or the new BOB thread?

The new thread, as per the answer to question 1.
Also, will I
be able to still receive data in my BOB thread, when the lengthy
database operation is performed?

Well, you won't be able to do anything else at the same time in the
same thread. The data will hopefully be queued - but that depends on
what your data source is.
3.Is this type of code safe?
4.Is it good practice? If not, what are the alternatives?

Having a loop that never exits is a bad idea IMO. Your event add/remove
code isn't thread-safe. You haven't given any reason to keep a
reference to the new thread. Other than that, it seems okay.

See http://www.pobox.com/~skeet/csharp/multithreading.html for more
information on threading in general though.
 
W

William Stacey [MVP]

As a side note, here is an alternate method that uses bounded blocking
queues instead of events. Also is a simple Listener and Server to network
send/receives and flow of packets between listener thread and server thread.
Server could have web methods, remoting, etc that allows gui mgr client or
console mgr client to monitor server, etc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top