c - 使用 pthread_create

标签 c pthreads

当我尝试使用 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/

相关文章:

c - 取消引用类型双关指针将打破严格的别名规则

c - 文件路径长度有多重要?

c++ - 许多类变量的 pthread

c++ - 在 C++ 中全局使用二维数组

c++ - 从另一个函数加入线程

c - fgetc 给我垃圾

c - 当我使用 c 中的套接字编程使用大文件时,性能如何提高

c - 如何在 C 中获取 Linux 命令位置?

c - 哲学家餐饮计划C

c++ - 如何修复 macOS pthread 互斥锁性能低下的问题?