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 205877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:35:44+00:00 2026-05-11T17:35:44+00:00

I recently wrote a program that used a simple producer/consumer pattern. It initially had

  • 0

I recently wrote a program that used a simple producer/consumer pattern. It initially had a bug related to improper use of threading.Lock that I eventually fixed. But it made me think whether it’s possible to implement producer/consumer pattern in a lockless manner.

Requirements in my case were simple:

  • One producer thread.
  • One consumer thread.
  • Queue has place for only one item.
  • Producer can produce next item before the current one is consumed. The current item is therefore lost, but that’s OK for me.
  • Consumer can consume current item before the next one is produced. The current item is therefore consumed twice (or more), but that’s OK for me.

So I wrote this:

QUEUE_ITEM = None

# this is executed in one threading.Thread object
def producer():
    global QUEUE_ITEM
    while True:
        i = produce_item()
        QUEUE_ITEM = i

# this is executed in another threading.Thread object
def consumer():
    global QUEUE_ITEM
    while True:
        i = QUEUE_ITEM
        consume_item(i)

My question is: Is this code thread-safe?

Immediate comment: this code isn’t really lockless – I use CPython and it has GIL.

I tested the code a little and it seems to work. It translates to some LOAD and STORE ops which are atomic because of GIL. But I also know that del x operation isn’t atomic when x implements __del__ method. So if my item has a __del__ method and some nasty scheduling happens, things may break. Or not?

Another question is: What kind of restrictions (for example on produced items’ type) do I have to impose to make the above code work fine?

My questions are only about theoretical possibility to exploit CPython’s and GIL’s quirks in order to come up with lockless (i.e. no locks like threading.Lock explicitly in code) solution.

  • 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-11T17:35:44+00:00Added an answer on May 11, 2026 at 5:35 pm

    Yes this will work in the way that you described:

    1. That the producer may produce a skippable element.
    2. That the consumer may consume the same element.

    But I also know that del x operation isn’t atomic when x implements del method. So if my item has a del method and some nasty scheduling happens, things may break.

    I don’t see a “del” here. If a del happens in consume_item then the del may occur in the producer thread. I don’t think this would be a “problem”.

    Don’t bother using this though. You will end up using up CPU on pointless polling cycles, and it is not any faster than using a queue with locks since Python already has a global lock.

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

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I've recently needed to write a script that performs an os.fork() to split into
I recently inherited a small Java program that takes information from a large database,
I've been working with some event handling in Javascript and recently decided to migrate
Recently I have been studying recursion; how to write it, analyze it, etc. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.