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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:40:19+00:00 2026-05-16T08:40:19+00:00

I’ve got an exam coming up soon. I’ve been looking through the past paper,

  • 0

I’ve got an exam coming up soon. I’ve been looking through the past paper, this question keeps bugging me. I can’t seem to find what the bug in the program could be because I’m quite new to all this. Can anyone help me out?

The following program contains a bug. Determine what kind of problem the program exhibits, and show how it can be fixed.

import threading
import time
import random
#the list "data" must contain two values.  
#The second must always be equal to the first multiplied by 4

class GeneratorThread(threading.Thread):
    #Thread for generating value pairs
    def __init__(self,data):
        threading.Thread.__init__(self)
        self.data=data
    def run(self):
        while True:
            #Pick a new first number
            num=random.randint(0,100)
            self.data[0]=num
            #simulate some processing 
          #to calculate second number
            time.sleep(1)
            #Place second value into ata
            self.data[1]=num*4
            time.sleep(1)
class ProcessorThread(threading.Thread):
    #Thread for processing value pairs
    def __init__(self,data):
        threading.Thread.__init__(self)
        self.data=data
    def run(self):
        while True:
            #Process current data
            num1=self.data[0]
            num2=self.data[1]
            print "Values are %d and %d."%(num1,num2)
            if num2!=num1*4:
                print "\tDATA INCONSISTENCY!"
            time.sleep(2)
if __name__=="__main__":
    data=[1,4]
    t1=GeneratorThread(data)
    t2=ProcessorThread(data)
    t1.start()
    t2.start()
  • 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-16T08:40:20+00:00Added an answer on May 16, 2026 at 8:40 am

    There’s a race condition between your two threads. Basically, there is a time between setting data[0] to something before setting data[1] to four times that value.

    If the second thread comes in and checks the values during that time, the data inconsistency will occur.

    You may think the two threads refer to their own data information but that’s not the case. They are both references to the main data array. If you really want them to have their own data arrays, you should change:

    self.data=data
    

    into:

    self.data=data[:]
    

    in both init functions.

    Otherwise (and this is the more likely case where you want to share the data), you need to ensure your threads are properly synchronised so that the data is always consistent, something like using a mutex:

    #!/usr/bin/python
    
    import threading
    import time
    import random
    #the list "data" must contain two values.
    #The second must always be equal to the first multiplied by 4
    
    class GeneratorThread(threading.Thread):
        #Thread for generating value pairs
        def __init__(self,data):
            threading.Thread.__init__(self)
            self.datamutex=datamutex         # save reference to the mutex.
            self.data=data
        def run(self):
            while True:
                #Pick a new first number
                num=random.randint(0,100)
                self.datamutex.acquire()     # get the mutex.
                self.data[0]=num
                #simulate some processing
              #to calculate second number
                time.sleep(1)
                #Place second value into ata
                self.data[1]=num*4
                self.datamutex.release()     # release it to allow other thread
                                             #  to run now that data is consistent.
                time.sleep(1)
    

     

    class ProcessorThread(threading.Thread):
        #Thread for processing value pairs
        def __init__(self,data):
            threading.Thread.__init__(self)
            self.datamutex=datamutex         # save mutex reference.
            self.data=data
        def run(self):
            while True:
                #Process current data
                self.datamutex.acquire()     # lock (can only happen if data consistent).
                num1=self.data[0]
                num2=self.data[1]
                self.datamutex.release()     # release it to allow updates.
    
                print "Values are %d and %d."%(num1,num2)
                if num2!=num1*4:
                    print "\tDATA INCONSISTENCY!"
                time.sleep(2)
    
    if __name__=="__main__":
        datamutex = threading.Lock()         # Create the mutex for both threads.
        data=[1,4]
        t1=GeneratorThread(data)
        t2=ProcessorThread(data)
        t1.start()
        t2.start()
    

    Now locks aren’t the only way to synchronise threads but they’re fine for this particular case.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.