经过一些实验,这似乎是在内核中模拟基本线程连接的最佳方法。我使用了completion方法,而不是semaphore方法,因为我发现它更简单。
struct my_thread_data {
struct completion *comp;
... // anything else you want to pass through
};
void *foo(void *arg) {
// doing something
return NULL;
}
int init_module(void) {
struct task_struct *threads[5];
struct completion comps[5];
struct my_thread_data data[5];
int i;
for (i=0; i<5; i++) {
init_completion(comps + i);
data[i].comp = comps + i;
thread[i] = kthread_run(&foo, (void*)(data + i), "ThreadName");
}
// wait here until all 5 threads are complete
for (i=0; i<5; i++) {
wait_for_completion(comps + i);
}
// do something else once threads are complete
return 0;
}