lab4-ex0

lab4-ex0 - //Create the threads. // These four dummy...

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){ v x /= 10; cout << "\nf2: divide x by 10 and becomes : " << x << endl; pthread_exit(NULL); } // subtract x by 2 void *func3 (void *arg){ v x -= 2; cout << "\nf3: subtract x by 2 and becomes : " << x << endl; pthread_exit(NULL); } // workload for dummy threads 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;
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: //Create the threads. // These four dummy threads must be created first for (int j=0; j&lt; 4; j++) pthread_create(&amp;thdummy, NULL, dummy, NULL); // These are the worker threads for the computation pthread_create(&amp;thread_id1, NULL, func1, NULL); pthread_create(&amp;thread_id2, NULL, func2, NULL); pthread_create(&amp;thread_id3, NULL, func3, NULL); //Wait for all threads terminate pthread_join(thread_id1, NULL); pthread_join(thread_id2, NULL); pthread_join(thread_id3, NULL); cout &lt;&lt; &quot;\nMain thread terminated&quot; &lt;&lt; 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-ex0 - //Create the threads. // These four dummy...

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