Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 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.  
  23. pthread_t m_thread[N_THREAD];
  24. int count = 0;
  25. pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
  26. pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
  27.  
  28.  
  29. // Main
  30. int main() {
  31.    
  32.     cout << "Launching main. \n";
  33.    
  34.     //Start to monitor for exceptions
  35.     register_exception_handler();
  36.    
  37.     //Start scheduler
  38.     scheduler();
  39.    
  40.     return 0;
  41. }
  42.  
  43. // Scheduler
  44. int scheduler() {
  45.     // Starting scheduler log file
  46.     ofstream scheduler_log;
  47.     scheduler_log.open ("scheduler_log.txt");
  48.     //scheduler_log << "[Scheduler] Starting." << endl;
  49.     cout << "[Scheduler] Starting.  \n";
  50.    
  51.     // Scheduler::Main Section
  52.      
  53.     int thread_id[N_THREAD];
  54.    
  55.     for(int i=0;i<N_THREAD;i++) {
  56.         thread_id[i] = i;
  57.         pthread_create( &m_thread[i], NULL, worker_thread, (void *) &thread_id[i]);
  58.     }
  59.  
  60.     for(int i=0;i<N_THREAD;i++)
  61.         pthread_join(m_thread[i], NULL);
  62.  
  63.    
  64.     cout << "[Scheduler] Ending. \n";
  65.     // Closing scheduler log file
  66.     scheduler_log.close();
  67.    
  68.     return 0;
  69. }
  70.  
  71. string atomic_output(int my_int, int thread_id) {
  72.     ostringstream stm;
  73.     stm << "Thread ";
  74.     stm << thread_id;
  75.     stm << ": ";
  76.    
  77.    
  78.     //count fn
  79.     stm << my_int;
  80.     stm << "\n";
  81.    
  82.    
  83.     //stm << "Finished. \n";
  84.    
  85.     return stm.str();
  86. }
  87.  
  88. void *worker_thread(void *ptr) {
  89.     string line;
  90.     //int boo = 0;
  91.    
  92.     int thread_id = *(int *) ptr;
  93.    
  94.     if(thread_id == 0)
  95.         pthread_mutex_lock( &count_mutex );
  96.    
  97.     for(int i=0;i<10;i++) {
  98.         //boo++;
  99.        
  100.         if (thread_id == 1) {
  101.             while(0)
  102.                 pthread_cond_wait( &condition_var, &count_mutex );
  103.         }
  104.    
  105.         if (thread_id == 3)
  106.             while(0)
  107.                 pthread_cond_wait( &condition_var, &count_mutex );
  108.        
  109.         if (i == 5) {
  110.             if(thread_id == 0) {
  111.                 pthread_mutex_unlock( &count_mutex );
  112.                 pthread_cond_broadcast(&condition_var);
  113.             }
  114.         }
  115.        
  116.         //count fn
  117.         line = atomic_output(i, *(int *)ptr);
  118.         cout << line;
  119.        
  120.     }
  121.    
  122.     //line = atomic_output(0, *(int *)ptr);
  123.     //cout << line;
  124. }
  125.  
  126. // Registers the first exception handler
  127. // Protection against hijack
  128. void register_exception_handler() {
  129.    
  130.     //
  131.     // register_virtual_exception_handler()
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement