void* request_routine(void* person) {
  //cout<<"In request routine\n\n"<<flush;
  int nameid = * (int *) person;
  string name = names[nameid];

  /* critical section, don't want multiple newthread requests at the same time,
   * or we get undefined behavior.
   */
  newthread_mutex.P();
  //cout<<"In critical section" << endl << flush;
  string newchan = chan->send_request("newthread");
  RequestChannel ochan(newchan, RequestChannel::CLIENT_SIDE);
  //cout<<"new request channel "<<newchan<<endl<<flush;
  newthread_mutex.V();
  /* end of critical section */

  cout<<"done"<<endl<<flush;

  string reply_str;
  int reply;
  string request = "data " + name;

  for (int i=0; i<REQUEST_SIZE; i++) {
    reply_str = ochan.send_request(request);
    reply = atoi(reply_str.c_str());
    buffer.produce(nameid, reply);
  }
  ochan.send_request("quit");
  pthread_exit(NULL);
}
Exemple #2
0
static void run(void* args){
	//assume args is an int
	int value = *(int*)args;

	s.P();
	cout << "This is run #" <<  value << " - counter #" << counter++ << endl;
	s.V();
}
void* statistic_routine(void* person) {
  int count = 0;
  int reply;
  const int nameid = *(int *) person;
  while (count < REQUEST_SIZE) {
    if (nameid == 0) {
      Joe_replies_mutex.P();
      if (!Joe_replies.empty()) {
        reply = Joe_replies.front();
        Joe_replies.pop();
        Joe_histogram[reply] += 1;
        count++; 
      }
      Joe_replies_mutex.V();
    }
    else if (nameid == 1) {
      Jane_replies_mutex.P();
      if (!Jane_replies.empty()) {
        reply = Jane_replies.front();
        Jane_replies.pop();
        Jane_histogram[reply] += 1;
        count++;
      }
      Jane_replies_mutex.V();
    }
    else if (nameid == 2) {
      John_replies_mutex.P();
      if (!John_replies.empty()) {
        reply = John_replies.front();
        John_replies.pop();
        John_histogram[reply] += 1;
        count++;
      }
      John_replies_mutex.V();
    }
    else {
      cerr << "Error in simpleclient.C::statistic_routine(): invalid nameid input.\n";
      pthread_exit(NULL);
    }
  }
  pthread_exit(NULL);
}
unsigned long SemaphoreThreadFunc(void* pParam)
{
	for (int i = 0; i < 10; ++i)
	{
		semaphore.P();
		nCounter++;
		cout << "Semaphore Thread ID: " << reinterpret_cast<int>(pParam) << ", \t Counter: " << nCounter << endl;
		semaphore.V();
	}
	return 0;
};
void* worker_routine(void* no_input) {
  vector<int> reply;
  while (true) {
    reply = buffer.consume();
    if (reply[0] == 0) {
      Joe_replies_mutex.P();
      Joe_replies.push(reply[1]);
      Joe_replies_mutex.V();
    }
    else if (reply[0] == 1) {
      Jane_replies_mutex.P();
      Jane_replies.push(reply[1]);
      Jane_replies_mutex.V();
    }
    else if (reply[0] == 2) {
      John_replies_mutex.P();
      John_replies.push(reply[1]);
      John_replies_mutex.V();
    }
    else {
      cerr<<"Error: In simpleclient.C's worker_routine(), bounded buffer id is invalid.\n";
    }
  }/* to be closed by parent thread */
}
Exemple #6
0
void * sig(void *ptr)
{
    sleep(1);
    Semaphore *sem;

    sem = (Semaphore*) ptr; 
    pthread_t   tid;
    tid = pthread_self();
    
    pthread_mutex_lock(&main_mutex);
    cout<<tid<<":start to sig"<<endl;
    pthread_mutex_unlock(&main_mutex);
    
    sem->V();
    
    pthread_mutex_lock(&main_mutex);
    cout<<tid<<":finish sig"<<endl;
    pthread_mutex_unlock(&main_mutex);
    
    return NULL;
}
Exemple #7
0
void
up(struct semaphore * sem)
{
    Semaphore* s = (Semaphore*)sem;
    s->V();
}
Exemple #8
0
void V(Semaphore& sem)
{
  sem.V();
}