1

In the following code snippet, I'm curious to know more about what is happening.

Assume that this code exists within the OnLoad() method of a Windows Service class and that MyClass is declared within a private property of this windows service class.

MyClass.Process1Method and MyClass.Process1
Dim process1 As System.Threading.Thread
Dim process2 As System.Threading.Thread
Dim ts As System.Threading.ThreadStart
ts = AddressOf MyClass.Process1Method
process1 = New System.Threading.Thread(ts)
process1.Start()
ts = AddressOf MyClass.Process2Method
process2 = New System.Threading.Thread(ts)
process2.Start()

Also assume that Process1Method and Process2Method both access some of the same private properties of MyClass.

So my question is, in case you haven't already guessed, are process1 and process2 threads going to fight over the same property values of MyClass simultaneously? Can you explain how this will behave?

If more clarification is needed I will try my best, Thanks.

PeonProgrammer
  • 1,445
  • 2
  • 15
  • 27
  • Yes. Welcome to the painful world of writing thread-safe code. http://blog.slaks.net/2013-07-22/thread-safe-data-structures/ – SLaks Jul 01 '14 at 16:18
  • @SLaks You wouldn't by chance have an up-to-date answer for my question here would you: http://stackoverflow.com/q/12574825/362536 Switching between the Node.js world and .NET, I understand why explicit threading code much more powerful and flexible, but for many tasks the simplicity of threading in Node.js, that my code is single-threaded but IO is automatically handled on separate threads, is sufficient. – Brad Jul 01 '14 at 16:23
  • @Brad: Just use nothing but async (`Task`-returning) methods, and don't use any actual threads. You will need an event loop (with a synccontext); you can either use a UI thread or one of several open-source event loop libraries. – SLaks Jul 01 '14 at 16:25
  • @SLaks Would you be willing to post a small example on an answer to my question? I'd happily award a bounty... and buy you beer if you're in the area. – Brad Jul 01 '14 at 16:28
  • @Brad: An example of what, exactly? – SLaks Jul 01 '14 at 17:47

0 Answers0