lab4-ex1

lab4-ex1 - void *dummy (void *arg) { int y=0; for (int i=0;...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <pthread.h> # using namespace std; u int x=7; i // Add 33 to the global variable x void *func1 (void *arg){ x += 33; cout << "\nf1: add 33 to x and becomes : " << x << endl; pthread_exit(NULL); } // Divide x by 10 void *func2 (void *arg){ pthread_t * tid; tid = (pthread_t *) arg; //wait for thread1 to terminate pthread_join(*tid, NULL); x /= 10; cout << "\nf2: divide x by 10 and becomes : " << x << endl; pthread_exit(NULL); } // subtract x by 2 void *func3 (void *arg){ pthread_t * tid; tid = (pthread_t *) arg; //wait for thread2 to terminate pthread_join(*tid, NULL); x -= 2; cout << "\nf3: subtract x by 2 and becomes : " << x << endl; pthread_exit(NULL); } // workload for dummy threads
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: void *dummy (void *arg) { int y=0; for (int i=0; i<536870912; i++) y += 3; y pthread_exit(NULL); } int main() { pthread_t thdummy, thread_id1, thread_id2, thread_id3; //Create the threads. // These four dummy threads must be created first for (int j=0; j< 4; j++) pthread_create(&thdummy, NULL, dummy, NULL); // These are the worker threads for the computation pthread_create(&thread_id1, NULL, func1, NULL); pthread_create(&thread_id2, NULL, func2, (void *) &thread_id1); pthread_create(&thread_id3, NULL, func3, (void *) &thread_id2); //Wait for thread 3 to terminate pthread_join(thread_id3, NULL); cout << "\nMain thread terminated" << endl; return 0; }...
View Full Document

This note was uploaded on 12/01/2010 for the course CS 2342 taught by Professor Dr during the Spring '10 term at HKU.

Page1 / 2

lab4-ex1 - void *dummy (void *arg) { int y=0; for (int i=0;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online