Ticket #9185: athread.cpp

File athread.cpp, 1.3 KB (added by aswin.paranji@…, 5 years ago)
Line 
1#include <stdio.h>
2#include <iostream>
3
4#include <stdlib.h>
5#include <pthread.h>
6#include <time.h>
7using namespace std;
8void *print_message_function( void *ptr );
9
10
11main()
12{
13     pthread_t thread1, thread2;
14     char *message1 = "Thread 1";
15     char *message2 = "Thread 2";
16     int  iret1, iret2;
17
18    /* Create independent threads each of which will execute function */
19
20     iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
21     iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
22
23     /* Wait till threads are complete before main continues. Unless we  */
24     /* wait we run the risk of executing an exit which will terminate   */
25     /* the process and all threads before the threads have completed.   */
26
27       
28     pthread_join( thread1, NULL);
29     pthread_join( thread2, NULL); 
30
31     printf("Thread 1 returns: %d\n",iret1);
32     printf("Thread 2 returns: %d\n",iret2);
33    // exit(0);
34}
35
36void *print_message_function( void *ptr )
37{
38     char *message;
39   struct timespec s;
40    s.tv_sec = 1;
41    s.tv_nsec = 500000000L;
42    nanosleep(&s, NULL);
43     message = (char *) ptr;
44     printf("%s \n", message);
45        for(int i=0;i<20;i++)
46                printf("%d",i);
47 printf("\n");
48try{
49        throw "EE";
50}
51catch(...){}
52
53getchar();
54}
55