Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <pthread.h>
- #include <unistd.h>
- using namespace std;
- void *print_message_function1(void *ptr);
- void *print_message_function2(void *ptr);
- int main(){
- cout << "nMain: Creating threads" << endl;
- cout << "Main: Waiting for threads to finish" << endl << endl;
- pthread_t thread1, thread2;
- char message1[] = " from Thread 1";
- char message2[] = " from Thread 2";
- int iret1, iret2;
- iret1 = pthread_create( &thread1, 0, print_message_function1, (void*) message1);
- iret2 = pthread_create( &thread1, 0, print_message_function2, (void*) message2);
- pthread_join(thread1, 0);
- pthread_join(thread2, 0);
- cout << "Thread 0 terminates" << endl;
- cout << "Thread 1 terminates" << endl;
- cout << "Main: Exiting" << endl;
- exit(0);
- }
- void *print_message_function1(void *ptr){
- char *message;
- message = (char*) ptr;
- for(int i=0; i<10; i++){
- cout << "Hello #" << i << message << endl;
- sleep(1);
- }
- }
- void *print_message_function2(void *ptr){
- char *message;
- message = (char*) ptr;
- for(int i=0; i<10; i++){
- cout << "Hello #" << i << message << endl;
- sleep(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement