I3ISU_Opgave4

Exercise 1 Creating Posix Threads
main.cpp: code format="cpp" void *threadFunction(void * id) {   for(int count = 0; count < 10; count++) {       printf("Hello #%d from thread %d\n", count, (int)id); //cout << "Hello #" << count << " from thread " << (int)id << endl; // Do not use! sleep(1); }   pthread_exit(NULL); }

int main {   pthread_t thread1, thread2;

pthread_create(&thread1, NULL, threadFunction, (void *)1); pthread_create(&thread2, NULL, threadFunction, (void *)2);

pthread_join(thread1, NULL); pthread_join(thread2, NULL);

return 0; } code When running our first version of the program we had an error that was caused by using cout in the threads. cout streams the text by using cascade-coupling, which means the threads have a opportunity to interrupt each other. This made the program very unstable. To fix the problem we used printf in the threads.

When compiling the program with g++ from the console remember to use: //g++ -pthread -o main main.cpp//.

When compiling the program with Eclipse you have to go to Project properties > C/C++ build > Settings > Miscellaneous (under GCC C++ Compiler) and type in //-pthread// under other flags. And put in //-pthread// as a library under Project properties > C/C++ build > Settings > GCC C++ Linker > Libraries.

If main returns completely after creating the threads the other threads will follow and terminate.

Exercise 2 Sharing data between threads
code format="cpp" int shared = 0;

void *reader(void*) {   for(int i = 0; i < 10; i++) {       printf("Current shared value: %d\n", shared); sleep(1); }   pthread_exit(NULL); }

void *incrementer(void*) {   for(int i = 0; i < 10; i++) {       shared++; sleep(1); }   pthread_exit(NULL); }

code we have chosen not to show the main function because all we do in it is creating and joining the two threads which we have already shown in exercise 1.

We do not know which of the two threads run at a given time. This means that the output gets a bit messy. Sometimes the reader outputs the same shared data (number) two times in a row and sometimes a number is skipped.

Exercise 3 Sharing a Vector class between threads
code format="cpp" Vector vec1;

void* writer(void* id) {   for(int i = 1; i < 101; i++) {       if(!vec1.setAndTest(i)) printf("Error found. Thread: %d, i: %d\n", (int)id, i); else printf("No Error found. Thread:%d, i:%d\n", (int)id, i);

usleep(1000000); }

pthread_exit(NULL); }

code Here we show the writer function with error checking and sleep function. The error checking makes it easy to identify any errors. But with 100 threads and 1 second sleep we had no errors.

Link to .cpp file.

Exercise 4 Tweaking parameters
The code is the same as in exercise 3. Except we play around with the number of threads and the amount of time in the sleep function. With 100 threads and 150 microseconds we start to get errors.

The errors occur when the sleep time is too short, and the computer cannot process all the information in time and the threads therefore corrupt eachother.

Exercise 5 Testing on target
Once again the code is the same as in exercise 3. Except we change the two parameters //number of threads// and //sleep function//. On target we get errors at 1 second with 100 threads. Target CPU is not powerful enough to run 100 threads without problems. Even 50 and 25 threads gives errors. At 10 - 15 threads the program runs stable and without errors.

Compared to the result in exercise 4 the target (which is a lot less powerful than the host) cannot run the program with anywhere near as many threads as the host can.