//******************Headers******* #include #include #include //*******************Headers****** using namespace std; using namespace tbb; #ifndef _monitor typedef spin_mutex Lock; Lock printing_lock; //*******struct node declaration******* struct node { int data; node *next; }; //*******enqueue and dequeue monitor class********************* class monitor_enqdeq { Lock qsmtx; node *front, *rear; public: monitor_enqdeq() : front(NULL), rear(NULL) { //front=rear= NULL; cout << "NEW parallel queue is created....." << endl; } bool empty() { return front == rear; } bool enqueue( const int &x )//enqueue function to enquee and returns bool value after enqueuing { node *new_node = new node; new_node->data=x; new_node->next=NULL; Lock::scoped_lock lock( qsmtx ); //spin_mutex lock... if (front==NULL) { rear=front=new_node; return false; } else { rear->next=new_node; rear=rear->next; return true; } } void print() //for printing elements in the queue at that instance.. { Lock::scoped_lock lock( qsmtx );//spin_mutex lock... Lock::scoped_lock lock2( printing_lock );//print lock for correct order printing.... cout << "printing: "; for ( node *tmp = front; tmp != NULL; tmp = tmp->next ) { cout << tmp->data << " "; } cout << endl; } bool dequeue( ) //for dequeuing elements..returns bool after dequeuing.. { Lock::scoped_lock lock( qsmtx ); //spin_mutex lock... node* tp = NULL; if ( ! front ) { return false; } tp = front; front = front->next; delete tp; tp = NULL; return true; } }; #define _monitor #endif