Threading question....

J

Justin

I am having trouble figuring out the best what to accomplish this fairly
simple goal. I have a program that controls a 1-wire network and I can only
make requests to it one at a time, but I will need to make new requests
(from multiple threads) before the first one is done. My idea is to use a
Queue to add requests to and then have another process (thread) that will
simply wait for the Queue to not be empty and then process requests one at a
time until it is empty and then again wait for the Queue to not be empty.

I'm looking for a basic framework of what would be best way to do this using
what Classes and how to signal between threads (if needed?). I would use
Queue, Thread? and ??

Any help is appreciated.

Norm
 
M

Michael D. Ober

Do you care about the order the requests are handled? If not, put your
network handler in a module (class or regular) and then use the following
code around your "critical section"

static SyncObj as new Object
Synclock SyncObj
' Process Critical Section
End SyncLock

Note that SyncObj is static as it must stick around for the life of the
application. What this does is stops all threads and forces them to wait
until the previous thread is complete with the network IO.

Mike Ober.
 
J

Justin

Mike,

Thanks for the input. However, that is exactly what I am doing now, and I
do (now) care about the sequence of requests. That is why I was thinking of
using the Queue class to hold requests. The main part I'm having trouble
with is in another thread processing those requests in the Queue and letting
the thread that added the request to the queue know that its request has
been fulfilled. And, also how to make the Queue processing thread 'sleep'
until the Queue.count is > 0.

As an aside... I was wondering if you know the difference between using
SyncLock/End SyncLock and Monitor.Enter/Monitor.Exit?

Thanks again for any input!

Norm
 
M

Michael D. Ober

Justin,

In this case, create a new class that inherits from the Queue class.
Override the Enqueue (Add) and Dequeue (Remove) methods and put a synclock
around the actual add and remove methods of the queue class. This way your
new class is thread safe.

I haven't used the Monitor.Enter/Monitor.Exit interface - SyncLock and
AutoResetEvents have been sufficient for my needs.

Mike.
 
J

Justin

Thanks Mike, I will try that....

Michael D. Ober said:
Justin,

In this case, create a new class that inherits from the Queue class.
Override the Enqueue (Add) and Dequeue (Remove) methods and put a synclock
around the actual add and remove methods of the queue class. This way
your
new class is thread safe.

I haven't used the Monitor.Enter/Monitor.Exit interface - SyncLock and
AutoResetEvents have been sufficient for my needs.

Mike.
 

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

Similar Threads

Threading in .Net... 4
Threading..... 4
Distributed application programming 3
threading 4
High performance UDP Listener? 2
Threading 1
Integrate a Queue with an ObjectPool 10
Unable to connect to the remote server 6

Top