COMP2355 lab11 solution

C++多线程程序作业代写,实现多线程任务分配

In this lab you’ll implement a program which uses multiple threads to divide up a simple problem

into independent sub-tasks. Source code for this lab is supplied in Lab11Source.zip .

Write the function ThreadedSum which is declared as:

double ThreadedSum(int numThreads, size_t arraySize);

You’ll find an empty function definition in main.cpp in the supplied source code. This function

should :

1. Allocate an array on the heap of type double with arraySize elements . This may get too large

for the stack, so make sure you allocate it on the heap (free store).

2. Run numThreads threads, each of which initializes an approximately equal portion of the

array. For example, if numThreads is 8, each thread should initialize 1⁄8 of the array. Make

sure you handle round-off correctly if arraySize is not an even multiple of numThreads. Each

element of the array should be initialized to the square root of the index of the element.

3. Wait for all of the initialization threads to complete,

Leave a Reply

Your email address will not be published. Required fields are marked *