Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:44:34+00:00 2026-06-14T22:44:34+00:00

Think about this code: #!/usr/bin/env python from threading import Thread count = 0 def

  • 0

Think about this code:

#!/usr/bin/env python

from threading import Thread

count = 0

def test():
    global count
    for i in range(10):
        count = count + 1

if __name__ == '__main__':
    for i in range(1000):
        Thread(target = test).start()
    print count

I use multiple threads, but the result is always correct. Does that mean I could use python threads without a lock when implementing something like a visitor counter ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:44:35+00:00Added an answer on June 14, 2026 at 10:44 pm

    You do need one. Although multithreading works differently in Python, due to the Global Interpreter Lock, operations that are not atomic in Python-bytecode will still need locking.

    In you case, you can inspect the bytecode for your function test (dis.dis(test)):

     3           0 SETUP_LOOP              30 (to 33)
                 3 LOAD_GLOBAL              0 (range)
                 6 LOAD_CONST               1 (1000)
                 9 CALL_FUNCTION            1
                12 GET_ITER
           >>   13 FOR_ITER                16 (to 32)
                16 STORE_FAST               0 (i)
    
     4          19 LOAD_GLOBAL              1 (count)   # start of increment
                22 LOAD_CONST               2 (1)
                25 BINARY_ADD
                26 STORE_GLOBAL             1 (count)   # end of increment
                29 JUMP_ABSOLUTE           13
           >>   32 POP_BLOCK
           >>   33 LOAD_CONST               0 (None)
                36 RETURN_VALUE
    

    As you can see, the increment is a 2xload, update, store on bytecode-level, so this wont work. The increment is actually 4 separate operations, which you must protect to ensure they are not interrupted.

    In your example the problem remains even if you use count += 1, as the bytecode shows:

    4          19 LOAD_GLOBAL              1 (count)
               22 LOAD_CONST               2 (1)
               25 INPLACE_ADD
               26 STORE_GLOBAL             1 (count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you think about data access code like this: public void AddCusotmer(Cusotmer customer)
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
I have a problem trying to abort a Python script, for example: #!/usr/bin/python import
I'd like to know what you think about this part of our program is
I've published my website many times. But didn't think about this though until I
I've been thinking about this and I just can't think of a way to
I think this question is really about my understanding of Garbage collection and variable
I think this is a moderately neophyte question. I've been using NHibernate/FluentNHibernate for about
this question is about style, because i think this is a very common problem
I have the following python modules. Sorry if the code is ugly. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.