I have general question,
exist one - Main thread, which get some data(for exple simple sting
packet).Exist big number of working threads(1000), which are waiting
for new data from Main thread.
Ho organise application(prototype), that main thread will notify to
all waiting threads that new data arrive and will not wait, that
working threads complete to read that data, that all working thread
will get immediately new data and not lose nothing?
Thanks
Just because you're asking about threading, that doesn't mean you have to
multi-thread your post.
As far as the question goes, you haven't provided enough information. But
you may find the Monitor class useful. Threads can Wait() and Pulse().
If you want multiple threads pulling data from the same queue, have the
producer thread (your "Main" thread) get the lock, add the data, then call
Pulse() and release the lock. This will then wake a thread, which will
then have the lock and can pull one item from the queue; if there's still
data in the queue, it can also then call Pulse() before releasing the lock
and getting on with the processing, which will wake another thread that
can do the same thing (i.e. each thread that wakes up will try to let
another thread also get going).
Using this technique, you'll also have to have each thread get the lock
and check the queue before going back to waiting, because the "pulsing"
isn't persistent. If no thread is already waiting, you lose the pulse.
If you like, you can instead use a WaitHandle (e.g. AutoResetEvent), to
provide a "data available" signal the threads can use. This can work
similar to pulsing, except that it's a persistent signal; if a thread sets
the event before proceeding with its own processing, and there's not yet
another thread waiting, the event will remain signaled so that when some
thread does try to wait on the event, it will be released immediately.
There are other techniques, each with their advantages and disadvantages.
But the two above approaches are among the simplest and should work fine
for most situations.
Note: you should only have a large number of worker threads (e.g. 1000) if
the work those threads need to do is i/o bound. You definitely don't want
1000 threads all trying to use the CPU at the same time.
Pete