why the following python program does not face any concurrencyproblems without synchronize mechanism

S

smith jack

from threading import Thread

def calc(start, end):
total = 0;
for i in range(start, end + 1):
total += i;
print '----------------------result:', total
return total

t = Thread(target=calc, args=(1,100))
t.start()

I have run this program for many times,and the result is always 5050,
if there is any concurrency problem, the result should not be 5050,
which is never met, anyhow
I mean this program should get the wrong answer at some times, but
this never happens, why?
can concurrency without synchronize mechanism always get the right answer?
any special case in python programming?
 
T

TheSaint

smith said:
have run this program for many times,and the result is always 5050
You might not need to make it in a multiprocess environment

Try it in the python (3) shell
.... tot += k
.... print(tot)
....

And watch the risults.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top