|
-
Feb 8th, 2010, 03:45 PM
#1
Thread Starter
Hyperactive Member
[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
-
Feb 8th, 2010, 03:45 PM
#2
Thread Starter
Hyperactive Member
Re: Threading Question
wow, please excuse the grammar.... I wrote that quickly.
-
Feb 8th, 2010, 03:53 PM
#3
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?
-
Feb 8th, 2010, 04:14 PM
#4
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 -
-
Feb 8th, 2010, 04:31 PM
#5
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
 
-
Feb 8th, 2010, 04:42 PM
#6
Thread Starter
Hyperactive Member
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
-
Feb 8th, 2010, 04:47 PM
#7
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
 
-
Feb 8th, 2010, 04:52 PM
#8
Thread Starter
Hyperactive Member
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?
-
Feb 8th, 2010, 04:56 PM
#9
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
 
-
Feb 8th, 2010, 05:19 PM
#10
Thread Starter
Hyperactive Member
Re: Threading Question
Shaggy,
Thanks very much! I'll get to writing!
- Nick
-
Feb 8th, 2010, 05:42 PM
#11
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|