Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <list>
- #include <map>
- #include <pthread.h>
- #include <fstream>
- #include <sstream> // for ostringstream
- #define N_THREAD 7
- using namespace std;
- // Prototypes
- int main();
- int scheduler();
- void register_exception_handler();
- void *worker_thread(void *ptr);
- string atomic_output(int my_int, int thread_id);
- // Global variables
- pthread_t m_thread[N_THREAD];
- int count = 0;
- pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
- // Main
- int main() {
- cout << "Launching main. \n";
- //Start to monitor for exceptions
- register_exception_handler();
- //Start scheduler
- scheduler();
- return 0;
- }
- // Scheduler
- int scheduler() {
- // Starting scheduler log file
- ofstream scheduler_log;
- scheduler_log.open ("scheduler_log.txt");
- //scheduler_log << "[Scheduler] Starting." << endl;
- cout << "[Scheduler] Starting. \n";
- // Scheduler::Main Section
- int thread_id[N_THREAD];
- for(int i=0;i<N_THREAD;i++) {
- thread_id[i] = i;
- pthread_create( &m_thread[i], NULL, worker_thread, (void *) &thread_id[i]);
- }
- for(int i=0;i<N_THREAD;i++)
- pthread_join(m_thread[i], NULL);
- cout << "[Scheduler] Ending. \n";
- // Closing scheduler log file
- scheduler_log.close();
- return 0;
- }
- string atomic_output(int my_int, int thread_id) {
- ostringstream stm;
- stm << "Thread ";
- stm << thread_id;
- stm << ": ";
- //count fn
- stm << my_int;
- stm << "\n";
- //stm << "Finished. \n";
- return stm.str();
- }
- void *worker_thread(void *ptr) {
- string line;
- //int boo = 0;
- int thread_id = *(int *) ptr;
- if(thread_id == 0)
- pthread_mutex_lock( &count_mutex );
- for(int i=0;i<10;i++) {
- //boo++;
- if (thread_id == 1) {
- while(0)
- pthread_cond_wait( &condition_var, &count_mutex );
- }
- if (thread_id == 3)
- while(0)
- pthread_cond_wait( &condition_var, &count_mutex );
- if (i == 5) {
- if(thread_id == 0) {
- pthread_mutex_unlock( &count_mutex );
- pthread_cond_broadcast(&condition_var);
- }
- }
- //count fn
- line = atomic_output(i, *(int *)ptr);
- cout << line;
- }
- //line = atomic_output(0, *(int *)ptr);
- //cout << line;
- }
- // Registers the first exception handler
- // Protection against hijack
- void register_exception_handler() {
- //
- // register_virtual_exception_handler()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement