Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C++ write a threaded program in which threads write data to a shared buffer. You must ensure that a race condition does not occur.

in C++

write a threaded program in which threads write data to a shared buffer. You must ensure that a race condition does not occur. You will write two versions of the program. The first version will use Pthreads (POSIX threads) and the second version will use C++ threads (which are built on top of Pthreads).

You should ask the user for a number and the number of threads to use. The main program will create the required number of threads. The numbers 1 to N will be partitioned among these threads so that two threads do not work on the same number. For each number in this set, the thread will determine if the number is a factor of N. The parent thread should wait until all the threads complete. The parent will then determine if the input number is perfect and report the result.

Implementation Notes

For both threaded versions you will need to write a function that can be used as the starting routine for the thread. This function will need to be given three arguments: the value of the number it is checking, the starting value for potential factors, and the ending value for potential factors. o You really only need to check factors between 1 and number / 2. Any number greater than number / 2 will not be a factor. o For example, if the number I am checking is 28 and I have requested 2 threads, one thread might check factors from 1 to 7 and the second thread might check factors from 8 to 14. For both implementations (Pthreads and C++ threads), you will need to link in the Pthread library. In CodeLite, you can do this by right-clicking on the project and selecting Settings. Choose the Linker tab and enter -lpthread in Linker Options. You may assume that the number we are checking can be stored in an uint64_t. Your threads should write the factors that they find to a shared buffer. Implement this buffer as an array of uint64_t. 2 o A buffer size of 100 should be sufficient for this project.

Sample input and output

input

Enter number: 8589869056

Number of threads: 7

output

Number of factors is: 33

8589869056 is a perfect number

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions