xxyzz / ostep-hw

Operating Systems: Three Easy Pieces(OSTEP) homework and project solutions
GNU General Public License v3.0
780 stars 181 forks source link

more concise barrier.c #16

Closed Winson-Huang closed 2 years ago

Winson-Huang commented 2 years ago

In barrier.c at Chap 31(31/barrier.c), you use two variable in barrier_t, but I think you can just keep num_threads and use num_threads--; when a thread enter barrier, then test num_threads==0 . Besides, in fact test of num_threads is not in critical section, so you can put Sem_post(b->modCounter); before the test.

Winson-Huang commented 2 years ago

If you like I can create a pr.

xxyzz commented 2 years ago

I put the test inside the mutex probably because the book(The Little Book of Semaphores) says it's usually not a good idea.

If you like I can create a pr.

OK, please.