Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在Linux内核中连接多个线程_C_Linux_Multithreading_Linux Kernel - Fatal编程技术网

C 如何在Linux内核中连接多个线程

C 如何在Linux内核中连接多个线程,c,linux,multithreading,linux-kernel,C,Linux,Multithreading,Linux Kernel,在继续之前,如何确保Linux内核中的多个线程已完成 请参见前面问题()中的示例代码(稍微修改) void*func(void*arg){ //做某事 返回NULL; } int init_模块(void){ struct task_struct*线程[5]; int i; 对于(i=0;i),经过一些实验后,这似乎是在内核中模拟基本线程连接的最佳方法。我使用了完成方法,而不是信号量方法,因为我发现它更简单 struct my_thread_data { struct completio

在继续之前,如何确保Linux内核中的多个线程已完成

请参见前面问题()中的示例代码(稍微修改)

void*func(void*arg){
//做某事
返回NULL;
}
int init_模块(void){
struct task_struct*线程[5];
int i;

对于(i=0;i),经过一些实验后,这似乎是在内核中模拟基本线程连接的最佳方法。我使用了完成方法,而不是信号量方法,因为我发现它更简单

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;
}
struct my\u thread\u数据{
结构完成*comp;
…//还有什么你想通过的吗
};
void*foo(void*arg){
//做某事
返回NULL;
}
int init_模块(void){
struct task_struct*线程[5];
结构完成补偿[5];
结构我的线程数据[5];
int i;

对于(i=0;i),经过一些实验后,这似乎是在内核中模拟基本线程连接的最佳方法。我使用了完成方法,而不是信号量方法,因为我发现它更简单

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;
}
struct my\u thread\u数据{
结构完成*comp;
…//还有什么你想通过的吗
};
void*foo(void*arg){
//做某事
返回NULL;
}
int init_模块(void){
struct task_struct*线程[5];
结构完成补偿[5];
结构我的线程数据[5];
int i;

对于(i=0;我必须逐个等待每个线程完成。当您在
for
循环中运行线程时,您也可以使用
for
循环等待每个线程完成。我不希望线程按顺序运行。线程应并行运行。按顺序等待线程完成绝不是按顺序运行e线程。线程仍然可以并行运行,并且可以以任何顺序完成。只需逐个等待每个线程的完成。当您在
for
循环中运行线程时,也可以使用
for
循环等待每个线程完成。我不希望线程按顺序运行。线程应该并行运行。顺序等待线程完成决不是按顺序运行线程。线程仍然可以并行运行,并且可以按任何顺序完成。