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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:50+00:00 2026-05-28T05:42:50+00:00

Multi-threading usually means locking critical sections, etc. So I can’t help but wonder, in

  • 0

Multi-threading usually means locking critical sections, etc. So I can’t help but wonder, in a single-threaded program or a multi-threaded program but where the queue is just used in one particular thread, is there some (unnecessary) locking type of overhead?

For instance when you call put or get or qsize, etc, does it lock then do some processing then release the lock?

  • 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-28T05:42:51+00:00Added an answer on May 28, 2026 at 5:42 am

    Locks are hard-coded into the Queue class. Thus, the methods put, get will use locks no matter how many threads exist in your program. The Queue is a module used to facilitate communications between threads.

    Check Queue.py‘s implementation

    class Queue:
        def __init__(self, maxsize=0):
            ...
            self.mutex = threading.Lock()
            self.not_empty = threading.Condition(self.mutex)
            self.not_full = threading.Condition(self.mutex)
            self.all_tasks_done = threading.Condition(self.mutex)
            self.unfinished_tasks = 0
    

    And its put method:

    def put(self, item, block=True, timeout=None):
        ...
        self.not_full.acquire()
        try:
            if self.maxsize > 0:
                ...
                elif timeout is None:
                    while self._qsize() == self.maxsize:
                        self.not_full.wait()
            self._put(item)
            self.unfinished_tasks += 1
            self.not_empty.notify()
        finally:
            self.not_full.release()
    

    I hope this answers your question.

    UPDATE:

    Even the method qsize uses locks:

    def qsize(self):
        """Return the approximate size of the queue (not reliable!)."""
        self.mutex.acquire()
        n = self._qsize()
        self.mutex.release()
        return n
    

    FYI, i checked Queue.py‘s implementation for Python2.7

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

Sidebar

Related Questions

I have a basic cs-major understanding of multi-threading but have never had to do
I am only somewhat familiar with multi-threading in that I've read about it but
I'm learning Multi Threading at the moment, in C#, but as with all learning
I've a multi-threading application in which each thread has to do some job, but
I want to work on multi-threading, but my current project do not have such
I'm not very 'up' on multi-threading, but I've been using detachNewThreadSelector in a couple
My question pertains to multi-threading in Java. I'm translating an app I wrote in
I am developing a multi-threading application, which is deadlocking. I am using Visual C++
I am using multi threading concept to run some process. this process uses the
I am relatively new to multi-threading and want to execute a background task using

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.