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

  • Home
  • SEARCH
  • 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 7606385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:23:48+00:00 2026-05-31T00:23:48+00:00

Let us suppose I have 2 threads and a single global variable in a

  • 0

Let us suppose I have 2 threads and a single global variable in a python code with the threading module.
In this code, only thread-1 modifies the global variable’s value, whereas, thread-2 only reads the value of the global variable and perform its task depending on that value.

In this situation, do I need to protect the access to the global variable with the Lock()? In C, a mutex must be used under such a condition. However, what about the python with GIL? Is it still the case? Is a Lock() still required?

  • 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-05-31T00:23:50+00:00Added an answer on May 31, 2026 at 12:23 am

    Assigning an object value to a global variable is an atomic operation in Python. Other threads cannot read a variable incorrectly by reading it while it’s being assigned. The GIL guarantees this in the C implementation of Python, but other implementations can and do make that same guarantee in different ways.

    If the global variable is a mutable object, like a list, and you are modifying that object, it depends on the method you use. Most methods on builtin objects like lists are atomic.

    I can’t say for sure that you don’t need a lock, though, without knowing more details about the purpose of that variable and how you are using it. Why does thread-2 need to change its behavior based on that value, and is it OK if thread-1 changes the value just after thread-2 has made its decision? If it’s not OK, you may still need a lock.

    (An analogous situation in C – assigning a value to a pointer variable – is also atomic under normal circumstances, though pointer assignments can be reordered. You can use this in some circumstances to avoid locks in C.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's suppose I have this piece of code. foreach(string value in myList) { string
Let's suppose I have the following code: TEModule teModule = Context.TEModules.Where(module => module.EnumValue.Equals(text.ModuleName)).FirstOrDefault(); if
Let's suppose I have n arrays, where n is a variable (some number greater
Let's suppose I have xml like this one: <Server Active=No> <Url>http://some.url</Url> </Server> C# class
Suppose I have some code: let listB = [ 1; 2; 3 ] Using
Let us suppose that I have a thread that consumes items produced by another
Let's suppose I have a string like this $str = this is my string;
Let's suppose I have a string that contains 7.2769482308e+01 (this number came from 3rd
Let's suppose I have a thread that locks on an object reference Thread #1
Let suppose that i have a hashmap like this Map map = new HashMap();

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.