Skip to content

Commit

Permalink
lab thread task 3
Browse files Browse the repository at this point in the history
  • Loading branch information
relaxcn committed Apr 6, 2024
1 parent b95669c commit 098ee56
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions notxv6/barrier.c
Expand Up @@ -25,12 +25,24 @@ barrier_init(void)
static void
barrier()
{
// YOUR CODE HERE
//
// Block until all threads have called barrier() and
// then increment bstate.round.
//

pthread_mutex_lock(&bstate.barrier_mutex); // Lock the mutex

// Increment the number of threads that have reached this round
bstate.nthread++;

// If not all threads have reached the barrier, wait
if (bstate.nthread < nthread) {
pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex);
}

// If all threads have reached the barrier, increment the round and reset the counter
if (bstate.nthread == nthread) {
bstate.round++;
bstate.nthread = 0;
pthread_cond_broadcast(&bstate.barrier_cond); // Notify all waiting threads
}

pthread_mutex_unlock(&bstate.barrier_mutex); // Unlock the mutex
}

static void *
Expand Down

0 comments on commit 098ee56

Please sign in to comment.