rohit35

Tnreads.cpp

Mar 31st, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include<time.h>
  5. #include<ctime>
  6. void *myThreadFun(void *vargp);
  7. void *my_date(void *thread);
  8. void *my_fun(void *type);
  9. // A normal C function that is executed as a thread
  10. // when its name is specified in pthread_create()
  11. void *myThreadFun(void *vargp)
  12. {
  13. printf("thread :%d\n",(int*)vargp);
  14. printf("Printing GeeksQuiz from Thread \n");
  15. // return NULL;
  16. }
  17.  
  18. int main()
  19. {
  20. pthread_t tid,tid_2,tid_3;
  21. // printf("Before Thread\n");
  22. pthread_create(&tid, NULL, myThreadFun, (void*)1);
  23. pthread_create(&tid_2,NULL,my_fun,(void*)2);
  24. pthread_create(&tid_3,NULL,my_date,(void*)3);
  25. pthread_join(tid, NULL);
  26. //printf("After Thread\n");
  27. }
  28. void *my_fun(void *type)
  29. {
  30. printf("Thread:%d\n",(int*)type);
  31. printf("random number\n");
  32. time_t t1;
  33. srand((unsigned) time(&t1));
  34. int random = rand() % 10;
  35. printf("rand=%d\n",random);
  36. // return NULL;
  37. }
  38.  
  39. void *my_date(void *thr)
  40. {
  41. printf("thread :%d\n",(int*)thr);
  42. time_t curr_time;
  43. curr_time = time(NULL);
  44.  
  45. char *tm = ctime(&curr_time);
  46. printf("Today date is %s\n",tm);
  47. printf("\n\n\t\t\tCoding is Fun !\n\n\n");
  48. //111return NULL;
  49. }
Add Comment
Please, Sign In to add comment