Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define NUM_THREADS 4
  6.  
  7. int sharedInt;
  8.  
  9. typedef struct {
  10. const char* userInput;
  11. }sharedValue;
  12.  
  13. void* threadListen(void* threadID){
  14. printf("thread %d live\n", threadID);
  15.  
  16. return 0;
  17. }
  18.  
  19. void* printText(void* foo){
  20. sharedValue *structure = (sharedValue*)foo;
  21.  
  22. printf("Thread received text: %s\n", foo);
  23.  
  24.  
  25. return 0;
  26. }
  27.  
  28.  
  29. void* mainThread(void* foo){
  30.  
  31. pthread_t threads[NUM_THREADS];
  32. int rc, i;
  33.  
  34. for(i = 0; i<NUM_THREADS;i++){
  35.  
  36. //Printing Thread
  37. if(i == 3)
  38. rc = pthread_create(&threads[i], NULL, printText, (void *)i);
  39.  
  40. //Consumer Threads
  41. else
  42. rc = pthread_create(&threads[i], NULL, threadListen, (void *)i);
  43. }
  44.  
  45.  
  46. if(rc){
  47. printf("pthread_create ERROR");
  48. return -1;
  49. }
  50.  
  51. printf("Please enter text: ");
  52. char textEntered[100];
  53. scanf("%99[^\r\n]", textEntered);
  54.  
  55. printf("%s", textEntered);
  56.  
  57. for (i = 0; i < NUM_THREADS; i++) {
  58. pthread_join(threads[i], NULL);
  59. }
  60.  
  61. return 0;
  62. }
  63.  
  64. int main(int argc, const char* argv[]){
  65.  
  66. int rc;
  67.  
  68. pthread_t main_thread;
  69.  
  70. //Producer Thread(Main Thread)
  71. rc = pthread_create(&main_thread, NULL, mainThread, NULL);
  72.  
  73. if(rc){
  74. printf("pthread_create ERROR");
  75. return -1;
  76. }
  77.  
  78. pthread_join(main_thread, NULL);
  79.  
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement