在SMP Linux内核中是否存在类似pthread_barrier的问题?
当内核同时在2个或更多具有相同结构的CPU上工作时,屏障(如pthread_barrier)可能很有用.它将阻止所有CPU进入它,直到最后一个CPU运行屏障.从这时起,所有CPU都会再次运行.
您可以使用完成获得等效行为:
struct fake_barrier_t {
atomic_t count;
struct completion comp;
}
/* run before each pass */
void initialize_fake_barrier(struct fake_barrier_t* b)
{
atomic_set(&b->count, 0);
init_completion(&b->comp);
}
/* make all tasks sleep until nth arrives, then wake all. */
void fake_barrier(struct fake_barrier_t* b, int n)
{
if (atomic_inc_return(&b->count) < n)
wait_for_completion(&b->comp);
else
complete_all(&b->comp);
}
Run Code Online (Sandbox Code Playgroud)