To get more experience with pthreads, and also to use timing functions in C.
An important part of parallel programming is load balancing. Ideally, each thread will spend the same amount of time working as each of the others.
For this lab, you will analyze the load balance between the threads of the $\pi$ estimation program.
To do this, simply have each thread time the amount of time it takes to execute.
In order to figure out how long a thread takes, we will need to get the time at two points, once at the start of the thread and once at the end. Then we can subtract the two values to find the elapsed time.
time() function we have seen has only second resolution.
In order to have more accurate numbers, we will find the number milliseconds
since the epoch, which is January 1st, 1970.
To do this, first include the header file
Next create an object of type
struct timeval current;
Then pass this object into the function
The second parameter is for passing time zone information, and should be NULL.
The timeval structure contains two fields,
tv_usec which contain the number of seconds and
To get the number of milliseconds, we can use the following:
unsigned long ms = (current.tv_sec * 1000) + (current.tv_usec / 1000);
When your program works, email the source code and the analysis to email@example.com.
Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.