Results 1 to 6 of 6

Thread: Queue with worker threads

  1. #1

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Queue with worker threads

    Hi,

    I have a bunch of objects that need processing by a script.
    What I'm doing now is the following:
    - Storing them in a queue
    - Letting the worker threads process them when they're available.

    I'm doing that by dequeueing from a synchronized instance of the queue.
    To quote the documentation:
    To guarantee the thread safety of the Queue, all operations must be done through the wrapper returned by the Synchronized method.
    But as I was afraid, it sometimes dequeues the same object twice.

    So, questions:
    1. Is myQueue.Dequeue() safe for usage without synchronization?
    2. If not, is this the following the correct fix for my problem?
    3. (if you have time) Could anyone explain to me what the lock() actually does?
    Does it pause all other threads hitting the same block until that block is finished?
    Code:
    lock (this._instanceLock)
    {
    	entity = this._eventQueue.Dequeue();
    }
    As always, thanks for any input
    Delete it. They just clutter threads anyway.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Queue with worker threads

    Don't use a a Queue at all. Just use the ThreadPool. Call QueueUserWorkItem and pass the object to be processed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Queue with worker threads

    Yes, I used the ThreadPool earlier, but there is a certain event that requires all threads to abort.
    Because the script is dynamic and can contain thread sleeping having a 'cancel'-flag is not an option here.

    Anyway, the snippet I posted earlier seems to do the trick, but is it actually correct usage there?
    Delete it. They just clutter threads anyway.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Queue with worker threads

    If you want a thread-safe Queue then you would do like this:
    csharp Code:
    1. Queue q = Queue.Synchronized(new Queue());
    and then enqueue and dequeue using 'q'. If you're going to use a 'lock' statement then you may as well use a generic Queue, which doesn't support synchronisation inherently.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Queue with worker threads

    Maybe I'm thinking of Queue<T> but I was pretty sure that Enqueue and Dequeue methods were safe to call from multiple threads without needing to synchronise.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Queue with worker threads

    Quote Originally Posted by Evil_Giraffe View Post
    Maybe I'm thinking of Queue<T> but I was pretty sure that Enqueue and Dequeue methods were safe to call from multiple threads without needing to synchronise.
    As with most collections, the generic Queue is less functional that its non-generic counterpart in this regard.
    Quote Originally Posted by MSDN
    To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width