Pages

Wednesday, September 26, 2012

ISU - Exercise 4 - Posix Threads

Posix Threads

Goal
  • learn how to create Posix threads
  • experience the shared data problem for multithreaded programs

1. Creating Posix Threads


















What happens if function main() returns immediately after creating the threads? Why?
The whole process will be killed including all the threads.
How did you pass thread ID?
We used the pthread data type: pthread_t which is a thread identifier. We passed it as a reference.

2. Sharing data between threads





Are there any problems in this program? Do you see any? why (not)?
A potentional problem could be that the resources are controlled by the OS and thus it is impossible to predict how much CPU time each thread will have. This could cause the reader to read the value shared before incrementer has incremented the value. A solution could be to reduce the time between incrementions. However we didn't notice this problem in our output.

3. Sharing a Vector class between threads

We developed the program so the threads test themselves each second for five seconds.
c++ file:



An example of the output:

4. Tweaking parameters

We added functionality to user define time delay in microseconds by using the usleep function.
We observe that there is no noticable difference if we use a shorter or longer time delay. The avearge number of errors is about the same whether we use 1 microsecond or a million microseconds.

Here are two outputs:


5. Testing on target








We experienced generally more errors when using shorter delay time than when using higher delay time. 1 second delay never gave us any errors whereas 1 microsecond delay consistently gave us 1 or 2 errors.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. In “exercise 1” it seems that the passing of the ID to the threads has been misunderstood. The threads are given the same ID (because the variable “value” never changes). Also, it prints out an ID from the function “pthreads_self()” on the terminal. This was not the goal of the exercise. The ID that should be used for the threads is the argument for “thread_function”.
    This is an important part of the exercise, as this helps to differentiate between the threads.

    The solution for “exercise 2” is within the goals of the exercise and shows how data is shared between threads.

    In “exercise 3”, “…4” and “…5” there are a lot of questions at the end of all the exercises. These are not being answered. Especially the questions about why the outcome turned out as it did. This is important to reflect about for the understanding of the exercise.

    In general the exercise solution is lagging in both the understanding of the ID passing in “exercise 1” and the text questions in “exercise 3”, “…4” and “…5”. But there is shown a great understanding for posix threads and it is just above the 66 %.

    ReplyDelete