Home All Groups Group Topic Archive Search About

Threading question....

Author
21 Jan 2006 8:47 PM
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

Author
22 Jan 2006 1:46 AM
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.

Show quoteHide quote
"Justin" <justin23856 @ hotmail> wrote in message
news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl...
> 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
>
>
>
Author
22 Jan 2006 2:18 AM
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


Show quoteHide quote
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net...
> 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.
>
> "Justin" <justin23856 @ hotmail> wrote in message
> news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>>
>
>
>
Author
23 Jan 2006 3:11 AM
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.

Show quoteHide quote
"Justin" <justin23856 @ hotmail> wrote in message
news:%23Mpf3ovHGHA.3944@tk2msftngp13.phx.gbl...
> 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
>
>
> "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
> news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net...
> > 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.
> >
> > "Justin" <justin23856 @ hotmail> wrote in message
> > news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl...
> >> 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
> >>
> >>
> >>
> >
> >
> >
>
>
>
Author
24 Jan 2006 7:14 PM
Justin
Thanks Mike, I will try that....

Show quoteHide quote
"Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
news:EPXAf.1919$Dk.808@newsread3.news.pas.earthlink.net...
> 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.
>
> "Justin" <justin23856 @ hotmail> wrote in message
> news:%23Mpf3ovHGHA.3944@tk2msftngp13.phx.gbl...
>> 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
>>
>>
>> "Michael D. Ober" <ober***@.alum.mit.edu.nospam> wrote in message
>> news:StBAf.1485$Dk.119@newsread3.news.pas.earthlink.net...
>> > 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.
>> >
>> > "Justin" <justin23856 @ hotmail> wrote in message
>> > news:e9MUxvsHGHA.2012@TK2MSFTNGP14.phx.gbl...
>> >> 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
>> >>
>> >>
>> >>
>> >
>> >
>> >
>>
>>
>>
>
>
>