locked
Threading issue RRS feed

  • Question

  • Hi All,


    I seem to be suffering from a multi-threading confusion.

    I have a TaskManager class that receives Tasks to be processed. 
    There is a limited number of tasks it allows to run (let's say 5) the rest get queued.

    Each Task object spawns its own processing thread and throws an event back to Task Manager
    indicating its completion.

    Upon completion, TaskManager removes processed task from the internal collection object and
    dequeus pending tasks moving them to processing list.

    Problem:  Somewhere things fall out of sync and processing stops.
    I do have the same event call back for ALL active Tasks.

    My guess is that while threads complete asynchronously the collection management piece
    gets out of what and everything stops.  After all collections such as Queue and HashTable are not
    thread-safe.

    I have an inkling this might be a threading issues since I have potentially different threads altering
    the same collection object on the TaskManager?


    Any input is appreciated.  Thanks!
    Friday, June 20, 2008 4:51 PM

Answers

  • pgq said:

    Hi All,


    I seem to be suffering from a multi-threading confusion.

    I have a TaskManager class that receives Tasks to be processed. 
    There is a limited number of tasks it allows to run (let's say 5) the rest get queued.

    Each Task object spawns its own processing thread and throws an event back to Task Manager
    indicating its completion.

    Upon completion, TaskManager removes processed task from the internal collection object and
    dequeus pending tasks moving them to processing list.

    Problem:  Somewhere things fall out of sync and processing stops.
    I do have the same event call back for ALL active Tasks.

    My guess is that while threads complete asynchronously the collection management piece
    gets out of what and everything stops.  After all collections such as Queue and HashTable are not
    thread-safe.

    I have an inkling this might be a threading issues since I have potentially different threads altering
    the same collection object on the TaskManager?


    Any input is appreciated.  Thanks!



    Without some code to go by, the only thing we can do is guess.  Based upon your description, I'm guessing that you haven't synchronized access to shared data and this data is getting corrupted by multiple thread access.
    http://www.peterRitchie.com/blog
    • Marked as answer by jack 321 Thursday, June 26, 2008 2:04 AM
    Friday, June 20, 2008 5:08 PM

All replies

  • If it were a problem with simultaneous access to one of your collections, you would get an exception indicating that. If that't not the case, I don't think that's the problem. Nevertheless, you should still synchronize access to those collections in your callbacks.

    Where exactly is the point of failure? Does it happen that the sixth task never runs? Is this a reliable failure? Have you tried fixing the length of time of tasks and synchronizing access to the collections and it still happens? I think you have to narrow down the exact condition and point where things break.
    Friday, June 20, 2008 5:02 PM
  • omarazam said:

    If it were a problem with simultaneous access to one of your collections, you would get an exception indicating that. If that't not the case, I don't think that's the problem. Nevertheless, you should still synchronize access to those collections in your callbacks.

    Where exactly is the point of failure? Does it happen that the sixth task never runs? Is this a reliable failure? Have you tried fixing the length of time of tasks and synchronizing access to the collections and it still happens? I think you have to narrow down the exact condition and point where things break.


    No, there likely wouldn't be an exception for simultaneous access to collections from multiple threads.
    http://www.peterRitchie.com/blog
    Friday, June 20, 2008 5:07 PM
  • pgq said:

    Hi All,


    I seem to be suffering from a multi-threading confusion.

    I have a TaskManager class that receives Tasks to be processed. 
    There is a limited number of tasks it allows to run (let's say 5) the rest get queued.

    Each Task object spawns its own processing thread and throws an event back to Task Manager
    indicating its completion.

    Upon completion, TaskManager removes processed task from the internal collection object and
    dequeus pending tasks moving them to processing list.

    Problem:  Somewhere things fall out of sync and processing stops.
    I do have the same event call back for ALL active Tasks.

    My guess is that while threads complete asynchronously the collection management piece
    gets out of what and everything stops.  After all collections such as Queue and HashTable are not
    thread-safe.

    I have an inkling this might be a threading issues since I have potentially different threads altering
    the same collection object on the TaskManager?


    Any input is appreciated.  Thanks!



    Without some code to go by, the only thing we can do is guess.  Based upon your description, I'm guessing that you haven't synchronized access to shared data and this data is getting corrupted by multiple thread access.
    http://www.peterRitchie.com/blog
    • Marked as answer by jack 321 Thursday, June 26, 2008 2:04 AM
    Friday, June 20, 2008 5:08 PM