简体   繁体   English

pthread_create int而不是void

[英]pthread_create int instead of void

I have the following code: 我有以下代码:

for(i = 0 ; i < max_thread; i++)
{
    struct arg_struct args;
    args.arg1 = file;
    args.arg2 = word;
    args.arg3 = repl;
    if(pthread_create(&thread_id[i],NULL,&do_process,&args) != 0)
    {
        i--;
        fprintf(stderr,RED "\nError in creating thread\n" NONE);
    }
}
for(i = 0 ; i < max_thread; i++)
    if(pthread_join(thread_id[i],NULL) != 0)
    {
        fprintf(stderr,RED "\nError in joining thread\n" NONE);
    }


int do_process(void *arguments)
{
//code missing
}

* How can I transform (void *)do_process into (int) do_process ?* * 如何将(void *)do_process转换为(int)do_process?*

That function returns very important info and without those returns I don't know how to read the replies 该函数返回非常重要的信息,如果没有这些返回,我不知道如何阅读回复

I get the following error: warning: passing arg 3 of `pthread_create' makes pointer from integer without a cast 我收到以下错误:警告:传递`pthread_create'的arg 3使得整数指针没有强制转换

The thread function returns a pointer. 线程函数返回一个指针。 At minimum, you can allocate an integer dynamically and return it. 至少,您可以动态分配一个整数并返回它。

void * do_process (void *arg) {
    /* ... */
    int *result = malloc(sizeof(int));
    *result = the_result_code;
    return result;
}

Then, you can recover this pointer from the thread_join() call; 然后,您可以从thread_join()调用中恢复此指针;

    void *join_result;
    if(pthread_join(thread_id[i],&join_result) != 0)
    {
        fprintf(stderr,RED "\nError in joining thread\n" NONE);
    } else {
        int result = *(int *)join_result;
        free(join_result);
        /* ... */
    }

Just write a helper function that is of the correct type, but all it does is take the void * input parameter, get all the right parameters out of it, call your function, take the return of that, and package it up as a void * for pthread_join to get. 只需编写一个类型正确的辅助函数,但它所做的就是获取void * input参数,从中获取所有正确的参数,调用函数,返回它,然后将其打包为void *为pthread_join获取。

To your specific question, you can't/shouldn't. 对于您的具体问题,您不能/不应该。 Just do what I outlined above and you'll be golden. 只要做我上面概述的内容,你就会变得金黄。

The pthread_join() is a simple way to communicate between the two threads. pthread_join()是两个线程之间通信的简单方法。 It has two limitations. 它有两个局限。 First, it can pass only one value from the pointer (you can make it a pointer and store multiple values). 首先,它只能从指针传递一个值(您可以将其作为指针并存储多个值)。 Second, you can return it only when the thread is all done -- after returning this value, the thread goes in terminated state. 其次,只有在线程全部完成后才能返回 - 在返回此值后,线程进入终止状态。 So, if you want the threads to communicate in a more granular fashion, you will be better served in using a common shared data. 因此,如果您希望线程以更精细的方式进行通信,则可以更好地使用公共共享数据。 Of course, at teh very least, you would to use Pthread mutex to synchronize access to the common data. 当然,至少在某种程度上,您可以使用Pthread互斥锁来同步对公共数据的访问。 And, if you want the threads to communicate with each other, then you would also need to use Pthread condvars. 而且,如果您希望线程彼此通信,那么您还需要使用Pthread condvars。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM