Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <list>
  4. #include <map>
  5. #include <pthread.h>
  6. #include <fstream>
  7.  
  8. #include <sstream> // for ostringstream
  9.  
  10. #define N_THREAD 7
  11.  
  12. using namespace std;
  13.  
  14. // Prototypes
  15. int main();
  16. int scheduler();
  17. void register_exception_handler();
  18. void *worker_thread(void *ptr);
  19. string atomic_output(int my_int, int thread_id);
  20.  
  21. // Global variables
  22. //pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7;
  23.  
  24. pthread_t m_thread[N_THREAD];
  25. int count = 1;
  26. pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
  27. pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
  28.  
  29.  
  30. // Main
  31. int main() {
  32.    
  33.     cout << "Launching main. \n";
  34.    
  35.     //Start to monitor for exceptions
  36.     register_exception_handler();
  37.    
  38.     //Start scheduler
  39.     scheduler();
  40.    
  41.     return 0;
  42. }
  43.  
  44.  
  45. // Scheduler
  46. int scheduler() {
  47.     // Starting scheduler log file
  48.     ofstream scheduler_log;
  49.     scheduler_log.open ("scheduler_log.txt");
  50.     //scheduler_log << "[Scheduler] Starting." << endl;
  51.     cout << "[Scheduler] Starting.  \n";
  52.    
  53.     // Scheduler::Main Section
  54.      
  55.     int thread_id[N_THREAD];
  56.    
  57.     for(int i=0;i<N_THREAD;i++) {
  58.         thread_id[i] = i;
  59.         pthread_create( &m_thread[i], NULL, worker_thread, (void *) &thread_id[i]);
  60.     }
  61.  
  62.     for(int i=0;i<N_THREAD;i++)
  63.         pthread_join(m_thread[i], NULL);
  64.  
  65.    
  66.     cout << "[Scheduler] Ending. \n";
  67.     // Closing scheduler log file
  68.     scheduler_log.close();
  69.    
  70.     return 0;
  71. }
  72.  
  73. string atomic_output(int my_int, int thread_id) {
  74.     ostringstream stm;
  75.     stm << "Thread ";
  76.     stm << thread_id;
  77.     stm << ": ";
  78.    
  79.    
  80.     //count fn
  81.     stm << my_int;
  82.     stm << "\n";
  83.    
  84.    
  85.     //stm << "Finished. \n";
  86.    
  87.     return stm.str();
  88. }
  89.  
  90. void *worker_thread(void *ptr) {
  91.     string line;
  92.     //int boo = 0;
  93.    
  94.     int thread_id = *(int *) ptr;
  95.    
  96.     //if(thread_id == 0)
  97.     //  pthread_mutex_lock( &count_mutex );
  98.    
  99.     for(int i=0;i<10;i++) {
  100.         //boo++;
  101.        
  102.         if (thread_id == 1) {
  103.  
  104.             pthread_mutex_lock(&count_mutex);
  105.             while (count == 1) {
  106.                 cout << "[Thread 1] Before pthread_cond_wait...\n";
  107.                 pthread_cond_wait( &condition_var, &count_mutex );
  108.                 cout << "[Thread 1] After pthread_cond_wait...\n";
  109.             }
  110.             pthread_mutex_unlock(&count_mutex);
  111.            
  112.         }
  113.    
  114.         if (thread_id == 3) {
  115.            
  116.             pthread_mutex_lock(&count_mutex);
  117.             while (count == 1) {
  118.                 cout << "[Thread 3] Before pthread_cond_wait...\n";
  119.                 pthread_cond_wait( &condition_var, &count_mutex );
  120.                 cout << "[Thread 3] After pthread_cond_wait...\n";
  121.             }
  122.             pthread_mutex_unlock(&count_mutex);
  123.         }
  124.        
  125.         //count fn
  126.         line = atomic_output(i, *(int *)ptr);
  127.         cout << line;  
  128.        
  129.         if (i == 5) {
  130.             if(thread_id == 0) {
  131.                 pthread_mutex_lock( &count_mutex );
  132.                 count = 0;
  133.                 pthread_mutex_unlock( &count_mutex );
  134.                 pthread_cond_broadcast(&condition_var);
  135.             }
  136.         }
  137.        
  138.  
  139.        
  140.     }
  141.    
  142.     //line = atomic_output(0, *(int *)ptr);
  143.     //cout << line;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement