Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying To Cause Problems

 
sean cotter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am playing around with threads trying to get a better understanding. In this case I am trying cause a variable to be corrupted. Unfortunately, is seems I am not sharing it but I do not understand why aren't I.




my output is:
booThread0 is starting!!
The value of I is 0
My thread name is booThread0
**** the value of i is: 0 - Name: booThread0
booThread1 is starting!!
The value of I is 0
My thread name is booThread1
**** the value of i is: 0 - Name: booThread1
booThread2 is starting!!
The value of I is 0
My thread name is booThread2
**** the value of i is: 0 - Name: booThread2
booThread3 is starting!!
The value of I is 0
My thread name is booThread3
**** the value of i is: 0 - Name: booThread3
**** the value of i is: 1 - Name: booThread2
**** the value of i is: 1 - Name: booThread3
**** the value of i is: 2 - Name: booThread2
**** the value of i is: 2 - Name: booThread3
**** the value of i is: 1 - Name: booThread0

Any ideas?
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What variable is supposed to be shared???

and there is only one thread running (except the main thread) so how could a racing condition happen?
 
sean cotter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lines 7-9 create 4 threads with names booThread0 thru booThread3, these names can be seen in the output.
The variable being shared is num located on 17 and modified on lines 17, 20 and 34.
Every time a new thread starts the value of num is 0 for that specific thread, as stated by the ouput lines "The value of I is 0", and the fact the the last 4 lines of output say the values for num are 1,2,2,1. Once in the loop I am always incrementing so it should never go from 2 to 1.

How would you modify it so other threads are sharing the same version on num?

Thank you for your help.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use static variables, so the same instance is shared between all classes ;-)
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at line 8, you will see that all four threads have there own copy of a TT1 instance. You will also notice that num is an instance variable, so all the four threads have there own copy of the num variable.

Two possible options to cause problems... (1) give all four threads the same TT1 instance, or (2) make the num variable a static variable.

Henry
 
sean cotter
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Henry!! I am now able to visualize how this works in my head.

I changed the main() to:



Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic