Threads and a local variable

I'm not sure if this is even a sensible question. I was wondering if the following code can ever print a 1.

And the output on several runs has always been: 0 0 0 0 0

My aim in this program is to let all the threads run line 1 and sleep. After that, I hoped to see a thread modify count and sleep. Now, the former scenario having occured, all the threads must print a value greater than 0. Am I right in my thinking? This problem is not a real-world development problem, its just a thing that came into my mind. [ November 17, 2006: Message edited by: Allen Sylvester ]

Purushoth Thambu

Ranch Hand

Posts: 425

posted 10 years ago

Each thread has it's own local copy. Local objects are allocated in thread's stack not in heap. So any modification you make on local member of one thread will not be visible to other thread. You need common shared object if you want threads to interact.

What you are trying to accomplish is this

[ November 17, 2006: Message edited by: Purushothaman Thambu ]

Sam Codean

Ranch Hand

Posts: 194

posted 10 years ago

try putting these instead and you will see what you want to

Your count was declared in the function stack which is not shared between instances you need to have it declared shared or static