void *threadFunc(void *arg){
	struct cart_t *cart;
	struct queue_info *info = (struct queue_info *)arg;
	fprintf(stderr, "thread for direction %c starts\n", info->dir);

  pthread_barrier_wait(&barrier);

	cart = q_getCart(info->dir);
	while(1){
    if(allFinished() == 1) break;
    if(cart == NULL){
      finishes[info->thread_index] = 1;
    }
    else{
      fprintf(stderr, "thread for direction %c gets cart %i\n", info->dir, cart->num);
      monitor_arrive(cart);
      monitor_cross(cart);
      monitor_leave(cart);
      cart = q_getCart(info->dir);
    }
	}

	fprintf(stderr, "thread for direction %c exits\n", info->dir);
	pthread_barrier_wait(&barrier);

	return (void *) 1;
}
/*
 *  Each thread repeatedly does this:
 *
 *  Try to get a cart off the head of the indicated queue.
 *  If there is no cart on that queue, return.
 *  If a cart is obtained, then:
 *     1. "arrive" at intersection -- may have to wait until signaled
 *     2. proceed into intersection once intersection is free & it becomes our turn
 *     3. leave intersection & signal closest non-empty queue to the right
 */
void *moveCart(void *args) {
  char direction = *(char *)args;
  struct cart_t *cart;

  fprintf(stderr, "thread for direction %c starts\n", direction);
  cart = q_getCart(direction);
  while (cart != NULL) {
    fprintf(stderr, "thread for direction %c removes cart %i from head of queue\n", direction, cart->num);
    monitor_arrive(cart);
    monitor_cross(cart);
    monitor_leave(cart);
    cart = q_getCart(direction);
  }
#if 1
  (void) pthread_barrier_wait(&endBarrier);
#endif
  fprintf(stderr, "thread for direction %c ends\n", direction);
  return NULL;
}
示例#3
0
void *west(void *arg)
{
  char direction;
  struct cart_t *cart;

  direction = 'w';

  fprintf(stderr, "thread for direction %c starts\n", direction);
  cart = q_getCart(direction);
  while (cart != NULL) {
    fprintf(stderr, "thread for direction %c gets cart %i\n",
	    direction, cart->num);
    monitor_arrive(cart);
    monitor_cross(cart);
    monitor_leave(cart);
    cart = q_getCart(direction);
  }

  fprintf(stderr, "thread for direction %c exits\n", direction);

  return NULL;
}
示例#4
0
/*
 *  Function that each direction thread runs.
 *  Gets the next cart from that direction queue if possible and passes it to the monitor.
 */
void *cart(void* args) {

    arg_t* actual;
    char direction;
    actual = (arg_t*) args;
    direction = actual->direction;
    struct cart_t *cart;

    fprintf(stderr, "\n** Direction %c thread starts **\n", direction);
    cart = q_getCart(direction);

    while (cart != NULL) {
        fprintf(stderr, "\nDirection %c thread gets cart %i\n", direction, cart->num);
        monitor_arrive(cart);
        monitor_cross(cart);
        monitor_leave(cart);
        cart = q_getCart(direction);
    }

    fprintf(stderr, "\n** Direction %c thread exits **\n", direction);

}