1
votes

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 ?*

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

3
I don't suppose showing at least the declaration of do_process is a possibility? but aside from that, is there some reason you can't put an arg_result into your args structure and convey your return result with that? - WhozCraig
Put the return value in args and return it. You can interrogate args on its return...use the 2nd param in pthread_join. - Duck
I think it's a great idea. What does second param in pthread_join? I set the value to NULL. - Sam Reina

3 Answers

2
votes

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;

    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);
        /* ... */
    }
1
votes

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.

To your specific question, you can't/shouldn't. Just do what I outlined above and you'll be golden.

0
votes

The pthread_join() is a simple way to communicate between the two threads. 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. And, if you want the threads to communicate with each other, then you would also need to use Pthread condvars.