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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:18:56+00:00 2026-05-16T22:18:56+00:00

I have the following consumer code: …. while 1: time.sleep(self.sleeptime) cond.acquire() #acquire the lock

  • 0

I have the following “consumer” code:

    ....

    while 1:

        time.sleep(self.sleeptime)

        cond.acquire() #acquire the lock
        print currentThread(), "lock acquired"

        while itemq.isEmpty():
            cond.wait()

        itemq.consume()
        print currentThread(),"Consumed One Item"
        cond.release()

And the following producer’s code:

....     
while 1 :


           cond.acquire() #acquire the lock
           print currentThread(), "lock acquired"
           print currentThread(),"Produced One Item"
           itemq.produce()
           cond.notifyAll()
           cond.release()

           time.sleep(self.sleeptime)

I’m running the program with 1 producer and 2 consumers.
I don’t know what result to expect. The producer calls “notifyAll()”, so I expect both consumers to wake up from their “wait”. I see that indeed both consumer acquire the lock, but only the first one who acquired the lock actually get to consume the item. Could somebody please tell me how the “wait” command works? If both threads get the “notifyAll”, how is it that only one gets to consume?

Thanks,
Li

  • 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-16T22:18:57+00:00Added an answer on May 16, 2026 at 10:18 pm

    The key is in the loop around the wait:

    while itemq.isEmpty():
            cond.wait()
    

    cond.wait() is implemented something like this (example only):

    def wait():
        cond.release()
        wait for notify
        cond.aquire()
    

    So only one consumer exits the ‘wait’ function at a time thanks to the lock. The first consumer to exit the wait function detects that itemq.isEmpty() == false and goes on to consume the item. They then re-enter the wait function and release the lock.

    The second consumer exits, detects that itemq.isEmpty() == true again, and re-enters wait() right away.

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

Sidebar

Related Questions

I have the following code (taken from actual script): $result=mysql_query($sql); while($row=mysql_fetch_assoc($result)) { $auth_token=$row['oauth_token']; $auth_token_secret=$row['oauth_token_secret'];
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have the following hypothetical code: class User < ActiveRecord::Base def oauth_consumer @oauth_consumer ||=
I have the following code. I m trying to access a queue and consume
I have to write this produce consumer application using multithreading. I wrote the following
I have the following code: #include <libubuntuone-1.0/u1-music-store.h> #include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-authentication.h> #include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-credentials.h> #include <libsyncdaemon-1.0/libsyncdaemon/syncdaemon-daemon.h> static
The Background I have the following source code #include <libubuntuone-1.0/u1-music-store.h> #include <libsyncdaemon-1.0/libsyncdaemon/libsyncdaemon.h> static void
I have the following code: import urlparse import oauth2 as oauth PROXY_DOMAIN = twitter1-ewizardii.apigee.com
I have written the following code in MATLAB to process large images of the
Have the following code: if (post) { HttpPost request = new HttpPost(url); Log.i(TAG, Posting

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.