gpt4 book ai didi

c - 使用 pthread_create

转载 作者:行者123 更新时间:2023-11-30 14:22:46 24 4
gpt4 key购买 nike

当我尝试使用 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_create()将填充thread_id字段。因此,首先,struct ms_args 的字段应该是 pthread_t 类型,并且您应该传递一个指向该字段的指针:

pthread_create(&argsLeft->thread_id, ...

关于c - 使用 pthread_create,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13536017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com