|
-
Jul 17th, 2004, 04:51 AM
#1
Thread Starter
Hyperactive Member
Multi-threaded synchronisation
Having programmed java for a couple of months now, I wanted to learn VB.NET. I thought I'd get really dug in immediately, since I know VB6 pretty well, and make a Dijkstra's philosophers program.
So, I'm gonna need multi-threading, but in preparation of coding this little bugger I really can't seem to find anything about threads not starting a function multiple times (as prevented by the java keyword synchronised). So, does anyone know how this is done in VB.NET? Does it happen automagicly?
Obey the dragon thing. Or not. Or possibly just a bit.
-
Jul 17th, 2004, 06:09 AM
#2
You will need 1 thread for each parralel instance of the function you want to run.
If you want to run the func 5 times simultaneously, you'll need 5 thread objects to do it.
I don't live here any more.
-
Jul 17th, 2004, 07:05 AM
#3
Thread Starter
Hyperactive Member
so if I have 5 threads (philosophers) which all have to make calls to the same method in some form somewhere, that outputs something to screen, each thread has to wait for another thread to finish with that method?
Obey the dragon thing. Or not. Or possibly just a bit.
-
Jul 17th, 2004, 09:50 AM
#4
yay gay
in c# it's:
lock {
}
in vb.net i believe it's "synlock"
\m/  \m/
-
Jul 17th, 2004, 10:57 AM
#5
Frenzied Member
Originally posted by The Dutch Dude
so if I have 5 threads (philosophers) which all have to make calls to the same method in some form somewhere, that outputs something to screen, each thread has to wait for another thread to finish with that method?
No, the threads do not HAVE to wait for the other thread to finish with the method, but if you want to make sure only one thread at a time accesses a block of code, you use (as PT Exorcist said), the lock statment (C#) or the SyncLock statment (VB).
You may or may not care if more than one thread is executing a block of code at the same time. For example, if you're just printing to the console, you probably don't care. If, however, the code is modifying the value of variables, you may want to ensure only one thread at a time is in that code. This can lead to some nasty bugs that are difficult to trace or duplicate.
Mike
-
Jul 17th, 2004, 11:50 AM
#6
Thread Starter
Hyperactive Member
thank you for your help. The method's going to be appending text to a textbox, the text therein can be considered as a global variable.
for those whom it may interest, the SyncLock keyword works as follows (MSDN quote):
VB Code:
SyncLock expression
...
End SyncLock
Parts:
expression
Required. A unique collection of operators and values that yield a single result.
block
Optional. The statements that will execute in sequence.
End SyncLock
Terminates a SyncLock procedure.
I've done some multi-threading in C and Java, so I know how hard debugging can be.
Obey the dragon thing. Or not. Or possibly just a bit.
-
Jul 17th, 2004, 12:21 PM
#7
Frenzied Member
FYI, you can run into problems when you're updating the GUI from a spawned thread. Reason is, as far as I understand it, is that the main (GUI) thread created the text box, but you're trying to modify it from a thread that does not own it. Probably not the best explanation there, but suffice to say, you need to code appropriately when modifying the GUI from a thread you created.
If you search, you'll find discussion on this, here's an article that may be of use http://msdn.microsoft.com/msdnmag/is...asicInstincts/
Have fun,
Mike
-
Jul 18th, 2004, 04:33 AM
#8
Thread Starter
Hyperactive Member
thx for the article, I think it'll come in handy.
If I encounter any problems I'll yet you guys (or girls) know.
Obey the dragon thing. Or not. Or possibly just a bit.
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
|