Results 1 to 11 of 11

Thread: [RESOLVED] Threading Question

  1. #1

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Resolved [RESOLVED] Threading Question

    Hey all,
    I was wondering if there was a posiblity of error if two seperate threads are accessing the same variables.

    i.e. I write several threads that gathers data from several communication ports (rs232, tcp etc) and puts those values in a public array of all data. I also have a thread that updates the display form with data all of the data from that array.

    Could this cause an error down the line with different threads reading and writing to the same variables?

    Thanks,
    Nick

  2. #2

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Re: Threading Question

    wow, please excuse the grammar.... I wrote that quickly.

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Threading Question

    Yes, the answer to your question is 'This could cause a whole lot of issues down the line with different threads reading and writing to the same variables?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Threading Question

    Could this cause an error down the line with different threads reading and writing to the same variables?
    The answer: YES.
    The fix: implement proper multi-threading techniques.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Threading Question

    Reading may cause you no trouble at all (though it may, too), but writing will DEFINITELY cause trouble.

    You need to make sure that only one thread is writing to the variable at any one time. This requires some form of synchronization. Look up Monitor and Synchlock in MSDN for some examples (there are others, too, such as Mutexes and Semaphores, but those first two are the ones you should reach for first until you determine that they won't work in some situation).


    As for reading, this will work as long as you understand that the value of any variable can change (due to the actions of a different thread) at any time, including during the execution of a line of code. For example:

    If a = 0 Then

    will work fine as long as your code can tolerate the fact that a might equal zero for that comparison, and not equal zero by the very next line of code (and sometimes it can change within a single line). Therefore, any read operation that requires a variable to remain in one state for even two lines of code (and sometimes even a single line) has to use synchronization control. If you don't, you will end up with a race condition, which is probably the most maddeningly difficult bug to track down, as it will be intermittent and inconsistent.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Re: Threading Question

    OK.
    So yeah; looks like will be a serious problem.

    What if I wrote a class that executed methods in a different thread that would raise an event when data is ready and through that event method transfer data?

    i.e. thread 1. Reads and parses incoming data from the serial port. When enough data is processed it raises an event that passes the processed data. A parent form would then handle the event and then display it.

    Then have similar events for classes that handle all of the other communication grabs?

    If all of the class methods are on separate threads... would this work ok?


    - Nick

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Threading Question

    Only local data is necessarily local to a thread. However, from your description, it looks like you are planning that each thread only deals with one set of data items, then passes them all by raising events. That would be fine (heck, I'm doing that myself in a couple programs).

    Keep in mind that an event raised by thread X is raised on thread X, so if you want a UI object to catch the events, you have to do something to raise the event on the UI thread. I do that using the SynchronizationContext, but there are other ways.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Re: Threading Question

    Ok,
    I'll investigate the SynchronizationContext. Before I google that, do you know off hand a good example of this sort of programming?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Threading Question

    If you look at post #7 in this thread:

    http://www.vbforums.com/showthread.p...&highlight=UDP

    you will see how I used SynchronizationContext in practice to do something quite similar to what you are proposing. You can ignore everything other than the lines using the SynchronizationContext object.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Re: Threading Question

    Shaggy,
    Thanks very much! I'll get to writing!
    - Nick

  11. #11

    Thread Starter
    Hyperactive Member nickwrs's Avatar
    Join Date
    Jan 2000
    Location
    Atlanta, Ga
    Posts
    398

    Re: [RESOLVED] Threading Question

    http://www.codeproject.com/KB/thread...?display=Print

    Just in case anyone else is curious. This was helpful along with Shaggy's example.

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