Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 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. void *worker_thread1(void *ptr);
  20. string atomic_output(int my_int, int thread_id);
  21.  
  22. // Global variables
  23. //pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7;
  24.  
  25. pthread_t m_thread[N_THREAD];
  26. int count = 1;
  27. pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
  28. pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;
  29.  
  30.  
  31. // Main
  32. int main() {
  33.    
  34.     cout << "Launching main. \n";
  35.    
  36.     //Start to monitor for exceptions
  37.     register_exception_handler();
  38.    
  39.     //Start scheduler
  40.     scheduler();
  41.    
  42.     return 0;
  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.             while(0) {
  104.                 pthread_mutex_lock(&count_mutex);
  105.                 if (count == 1)
  106.                     pthread_cond_wait( &condition_var, &count_mutex );
  107.                 pthread_mutex_unlock(&count_mutex);
  108.             }
  109.         }
  110.    
  111.         if (thread_id == 3)
  112.             while(0) {
  113.                 pthread_mutex_lock(&count_mutex);
  114.                 if (count == 1)
  115.                     pthread_cond_wait( &condition_var, &count_mutex );
  116.                 pthread_mutex_unlock(&count_mutex);
  117.             }
  118.        
  119.         if (i == 5) {
  120.             if(thread_id == 0) {
  121.                 pthread_mutex_lock( &count_mutex );
  122.                 count = 0;
  123.                 pthread_mutex_unlock( &count_mutex );
  124.                 pthread_cond_broadcast(&condition_var);
  125.             }
  126.         }
  127.        
  128.         //count fn
  129.         line = atomic_output(i, *(int *)ptr);
  130.         cout << line;
  131.        
  132.     }
  133.    
  134.     //line = atomic_output(0, *(int *)ptr);
  135.     //cout << line;
  136. }
  137.  
  138.  
  139. void *worker_thread1(void *ptr){
  140.     for(int i=0;i<40;i++)
  141.         cout << i << endl;
  142. }
  143.  
  144. // Registers the first exception handler
  145. // Protection against hack hijack
  146. void register_exception_handler() {
  147.    
  148.     //
  149.     // register_virtual_exception_handler()
  150. }
  151.  
  152. /*
  153. Launching main.
  154. [Scheduler] Starting.
  155. Thread 0: 0
  156. Thread 1: 0 <--- Again, no respect for the condition, count = 1
  157. Thread 3: 0
  158. Thread 3: 1
  159. Thread 3: 2
  160. Thread 3: 3
  161. Thread 3: 4
  162. Thread 3: 5
  163. Thread 3: 6
  164. Thread 3: 7
  165. Thread 3: 8
  166. Thread 3: 9
  167. Thread 0: 1
  168. Thread 0: 2
  169. Thread 0: 3
  170. Thread 0: 4
  171. Thread 2: 0
  172. Thread 2: 1
  173. Thread 2: 2
  174. Thread 2: 3
  175. Thread 2: 4
  176. Thread 2: 5
  177. Thread 2: 6
  178. Thread 2: 7
  179. Thread 2: 8
  180. Thread 2: 9
  181. Thread 0: 5
  182. Thread 0: 6
  183. Thread 0: 7
  184. Thread 0: 8
  185. Thread 0: 9
  186. Thread 6: 0
  187. Thread 1: 1
  188. Thread 1: 2
  189. Thread 1: 3
  190. Thread 1: 4
  191. Thread 1: 5
  192. Thread 1: 6
  193. Thread 1: 7
  194. Thread 1: 8
  195. Thread 1: 9
  196. Thread 6: 1
  197. Thread 6: 2
  198. Thread 6: 3
  199. Thread 6: 4
  200. Thread 6: 5
  201. Thread 6: 6
  202. Thread 6: 7
  203. Thread 6: 8
  204. Thread 6: 9
  205. Thread 5: 0
  206. Thread 5: 1
  207. Thread 5: 2
  208. Thread 5: 3
  209. Thread 5: 4
  210. Thread 5: 5
  211. Thread 4: 0
  212. Thread 4: 1
  213. Thread 4: 2
  214. Thread 5: 6
  215. Thread 5: 7
  216. Thread 5: 8
  217. Thread 5: 9
  218. Thread 4: 3
  219. Thread 4: 4
  220. Thread 4: 5
  221. Thread 4: 6
  222. Thread 4: 7
  223. Thread 4: 8
  224. Thread 4: 9
  225. [Scheduler] Ending.
  226. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement