Results 1 to 8 of 8

Thread: Multi-threaded synchronisation

  1. #1

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261

    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.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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.

  3. #3

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261
    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.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    in c# it's:

    lock {
    }

    in vb.net i believe it's "synlock"
    \m/\m/

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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

  6. #6

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261
    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:
    1. SyncLock expression
    2. ...
    3. 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.

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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

  8. #8

    Thread Starter
    Hyperactive Member The Dutch Dude's Avatar
    Join Date
    Nov 2001
    Location
    Holland. Land of the Dutch. Home for me.
    Posts
    261
    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
  •  



Click Here to Expand Forum to Full Width