Write a multithreaded program that demonstrates RLock. Create two thread classes—Ad- dThread and MultiplyThread. Class AddThread should increment a global variable by 1, sleep for two seconds and increment the variable again. Class MultiplyThread should double the global variable,sleep for two second and double the variable again. Protect these critical sections with a lock. After both threads

terminate, print the final value of the shared variable. Also, try commenting out lines so that the lock is not used. Notice the different value the variable has.

What will be an ideal response?


```
# Using an RLock to protect access to a critical section.

import threading
import time

class AddThread( threading.Thread ):
"""Subclass of threading.Thread"""

def __init__( self, threadName, lock ):
"""Initialize thread"""

threading.Thread.__init__( self, name = threadName )
self.lock = lock

def run( self ):
"""Modify data, sleep and modify again"""

global number

self.lock.acquire() # protect critical section

print self.getName(), "starting:", number
number += 1
print self.getName(), "sleeping:", number
time.sleep( 2 )
number += 1
print self.getName(), "done:", number

self.lock.release() # allow access to critical section

class MultiplyThread( threading.Thread ):
"""Subclass of threading.Thread"""

def __init__( self, threadName, lock ):
"""Initialize thread"""

threading.Thread.__init__( self, name = threadName )
self.lock = lock

def run( self ):
"""Modify data, sleep and modify again"""

global number

self.lock.acquire() # protect critical section

print self.getName(), "starting:", number
number *= 2
print self.getName(), "sleeping:", number
time.sleep( 2 )
number *= 2
print self.getName(), "done:", number

self.lock.release() # allow access to critical section

number = 1
lock = threading.RLock()

thread1 = AddThread( "thread1", lock )
thread2 = MultiplyThread( "thread2", lock )

thread1.start()
thread2.start()

thread1.join()
thread2.join()

print "Number is:", number
```
thread1 starting: 1
thread1 sleeping: 2
thread1 done: 3
thread2 starting: 3
thread2 sleeping: 6
thread2 done: 12
Number is: 12
thread1 starting: 1
thread1 sleeping: 2
thread2 starting: 2
thread2 sleeping: 4
thread1 done: 5
thread2 done: 10
Number is: 10

Computer Science & Information Technology

You might also like to view...

Computers use special computer codes that are based on ones (1s) and ____.

A. bit B. zeros C. twos D. letters

Computer Science & Information Technology

The size of the paper being used determines the number of columns that should be used

Indicate whether the statement is true or false

Computer Science & Information Technology

Direct access storage devices (DASDs) are devices that can directly read or write to an arbitrary place on a disk.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

During the implementation phase, the organization translates its blueprint for information security into a project ____________________.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology