Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.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 使用pthread_创建_C_Pthreads - Fatal编程技术网

C 使用pthread_创建

C 使用pthread_创建,c,pthreads,C,Pthreads,尝试使用pthread_create时出错。我知道我对argsRight->thread_id/argsLeft->thread_id和NULL的使用是不正确的,但我不确定如何引用线程id。它需要一个指针,但似乎我尝试过的每种方法(&,*)GCC编译器都不会接受 还有,它有什么理由不接受我使用NULL吗?我看不出任何错误的原因,但GCC说我对void函数的使用是无效的 有人能解释一下如何正确设置对pthread_create的调用吗?我已经包含了使用pthread_create函数的方法中的部分

尝试使用pthread_create时出错。我知道我对argsRight->thread_id/argsLeft->thread_idNULL的使用是不正确的,但我不确定如何引用线程id。它需要一个指针,但似乎我尝试过的每种方法(&,*)GCC编译器都不会接受

还有,它有什么理由不接受我使用NULL吗?我看不出任何错误的原因,但GCC说我对void函数的使用是无效的

有人能解释一下如何正确设置对pthread_create的调用吗?我已经包含了使用pthread_create函数的方法中的部分

void pthreads_ms(struct ms_args* args)
{
int left_end = (args->end + args->start) / 2;
int right_start = left_end + 1;
int rc1, rc2;

// Create left side struct
struct ms_args* argsLeft;
argsLeft = malloc(sizeof(args));
argsLeft->thread_id = (2 * args->thread_id + 1);
argsLeft->start = args->start;
argsLeft->end = left_end;
argsLeft->array = args->array;

// Same methodology as above to create the right side

if (args->start != args->end)
{
        // Print the thread id number, and start and end places
            printf("[%d] start %d end %d", args->thread_id, args->start, args->end);

        // Sort Left Side
        rc1 = pthread_create(argsLeft->thread_id, NULL, pthreads_ms(argsLeft), argsLeft);   //problem line here

        //Sort right side
        rc2 = pthread_create(argsRight->thread_id, NULL, pthreads_ms(argsRight), argsRight); //problem line here
}

它不是您的应用程序,而是pthread\u create()将填充thread\u id字段。因此,首先,struct ms_args的字段应为pthread_t类型,并且应向该字段传递指针:

pthread_create(&argsLeft->thread_id, ...
根据适当的要求应该是

rc1 = pthread_create(&(argsLeft->thread_id), NULL, &pthreads_ms, argsLeft);
右边也是一样

pthread_ms()
的定义应包含一个返回值

void *pthreads_ms(struct ms_args* args) { ... }

除此之外,您的代码对我来说相当危险,因为它会为每个现有线程递归创建两个线程。根据您的输入,这可能会构建一个大的线程树,这可能会导致系统停止。

如何定义
struct ms_args
argsLeft->thread_id=(2*args->thread_id+1)???