Question: The attached synch_count.py Python3 file is an incomplete program the student will have to finish. The program takes in a --thread_count command line argument. Upon

The attached synch_count.py Python3 file is an incomplete program the student will have to finish. The program takes in a --thread_count command line argument. Upon successful completion, the program will output the following 3 values:

  • total_count
  • double_count
  • triple_count

The three count variables are to be incremented in the inc_counts() method. For each invocation of inc_counts(), the total_count variable should be incremented by 1, the double_count variable should be incremented by 2, and the triple_count variable should be incremented by 3.

The purpose of this exercise is gain some basic exposure with using mutex locks to protect critical sections of code being accessed by multiple threads and barriers to coordinate multiple threads.

The attached file already has code that processes command-line arguments, starts the required number of threads, and outputs the final result.

Inside the file are four TODO sections. The student must address each of the TODO sections so that the program works correctly. There is a description associated with each TODO as to what needs to be done for that section. To fully address the TODO sections, the student will have to use the Barrier and Lock classes found in the threading module.

Below is an example of a correct implementation being executed:

python3 synch_count.py --thread_count 5

Launching [5] threads...

Final single_count=500000

Final double_count=1000000

Final triple_count=1500000

--------------------THE GIVEN CODE-----------------------

import argparse from threading import Thread # global variable accessed by multiple threads single_count = 0 double_count = 0 triple_count = 0 # TODO: declare any other global variables you may need here # TODO: implement the inc_counts method so that for each invocation: # single_count is incremented by 1 # double_count is incremented by 2 # triple_count is incremented by 3 # NOTE: INCREMENTS MUST BE DONE IN THE THREAD-SAFE MANNER USING threading.Lock def inc_counts(): def thread_method(): for _ in range(100000): inc_counts() # TODO: have all threads wait on a threading.Barrier until all have completed if __name__ == '__main__': parser = argparse.ArgumentParser(description='Synch Count') parser.add_argument('--thread_count', type=int) args = parser.parse_args() print("Launching [%d] threads..." % args.thread_count) # Create new threads thread_list = [] for count in range(args.thread_count): t = Thread(target=thread_method) t.start() thread_list.append(t) # TODO: have main thread wait on a threading.Barrier until all threads have completed # NOTE: The total thread count the Barrier will have to monitor is args.thread_count + 1. # When constructing the Barrier instance, keep in mind the "main" thread executing code in addition to any other # threads that are spawned to call thread_method() # Should be done waiting on Barrier, time to print the results print("Final single_count=%d" % single_count) print("Final double_count=%d" % double_count) print("Final triple_count=%d" % triple_count) 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!