, , ,

[SOLVED] Cop4600 ex5: threading

$25

File Name: Cop4600_ex5__threading.zip
File Size: 207.24 KB

5/5 - (1 vote)

Overview The aim of this exercise is to familiarize you with multithreading in C++. In the right situation, threads can significantly speed up your programs by allowing parts of it to run concurrently rather than sequentially. Here is a simple example: Note the use of the join() function above. Without it, it would be possible for the main function to finish executing and exit before the thread has completed execution. Structure To complete this exercise, you will be writing a program that spawns 10 threads, each of which will attempt to do the same job. Due to the nature of threads, you’ll notice that they finish in a different order every time. Your program must not block for any reason. 1. Write a C++ program that takes an integer as a command-line argument (i.e. ./threads 1414). Your program must accept exactly one argument. You may assume that any argument passed to your program will be valid (no input validation needed). 2. Write a function inside this program with two integer parameters: an ID number and a target. a. This function will generate random integers between 0 and 9999 until it generates one that matches the target number, then print “Thread completed.” 3. In your program’s main function, spawn 10 threads, each of which will call your new function with a unique ID (0 through 9) and the number given as a command-line argument as the target. Note: Your threads must be spawned inside of a loop. You should NOT have 10 individual calls to std::thread() in your program. 4. Finally, once all of your threads have finished generating numbers, print “All threads have finished finding numbers.” Note: To compile your program with multithreading, use the following command (this is how we will build your submission): g++ threads.cpp -o ex5.out -pthread -std=c++11 Enabling Race Conditions To ensure that the threads finish executing in a random order, you can use the nice utility. nice is a program that allows setting or altering the priority of a process. You should give your process the lowest priority to ensure that the CPU will execute it less often. In addition, you should lower your virtual machine’s memory and maximize the number of cores. This will slow the process down and cause the threads to execute out of order due to the interruptions and race conditions. Read about nice here: https://man7.org/linux/man-pages/man1/nice.1.html Note: RAM and CPU settings differ depending on your machine. You should give your virtual machine at least 512 MB. Submissions You will submit the following at the end of this exercise on Canvas: ⚫ C++ source file for your program (threads.cpp) ⚫ Screenshot of the output from running your program twice (output.png) Figure 1: An example screenshot with output from running the program twice.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] Cop4600 ex5: threading[SOLVED] Cop4600 ex5: threading
$25