-1
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct Array 
{
    //
};
void* evensum(void* param)
{
    //calculated the sum of even elements and returned it
}
void* oddsum(void* param)
{ 
  //did the same thing but for odd elements
}
int main()
{
    struct Array* obj=malloc(sizeof(struct Array));
    //did all the inputs
    int evensum,oddsum; 
    pthread_t thread1,thread2;
    pthread_create(&thread1,0,&evensum,(void*)obj);
    int evensum,oddsum;
    pthread_join(&thread,(void**)evensum);
    pthread_create(&thread2,0,&oddsum,(void*)obj);
    pthread_join(&thread2,(void**)oddsum);
    //try to print it using %i but I get or  %d 
    // I get the sum as zero
}

So I created two separate threads and these threads were meant to work asynchronously. I followed the advice mentioned here but the join still doesn't work as once the thread1 finishes execution, the other thread is never created despite me following the correct syntax. Any idea how to fix this? Also, the value printed is zero, despite showing the correct value if I print it in the function. Here is what I wrote in the return statement of each function:

return (void*)sum;//variable that stores sum

Also, I want to add that I don't want to use semaphores or any other synchronization tool to do this.

Anshuman Kumar
  • 464
  • 1
  • 6
  • 20
  • what's your real problem "the other thread is never created " what does it mean? it crashes? it exits? computer catches fire? something else? – Jean-François Fabre Oct 03 '18 at 16:08
  • What I meant is that the function meant for the thread is never called, and the terminal just stops after printing the sum of the even elements, which was just after pthread_join(&thread,(void**)evensum). It prints the value 0 and stops right there and the main thread doesn't terminate. Sorry if I confused you cause I had omitted the line for brevity. – Anshuman Kumar Oct 03 '18 at 16:18

2 Answers2

1

The second parameter to pthread_join is a void **, i.e. it expects a pointer to a void * which it dereferences to store the value in. Because you're not passing the address of a variable, this function will attempted to use whatever value you passed in as an address (which would likely be invalid) and dereference it. This invokes undefined behavior.

Also, the first parameter is of type pthread_t but you're passing a pointer to a pthread_t.

You're also declaring local variables with the same names as the functions you're calling. As a result, when you call pthread_create(&thread2,0,&oddsum,(void*)obj); you're actually passing the local int variable called oddsum and not the function called oddsum. That's why it hangs.

Changing the name of the variables to hold the results, changing the pthread_join calls to pass the addresses of these variables and passing the thread IDs directly should work:

int evenresult;
pthread_join(thread,(void**)&evenresult);
...
int oddresult;
pthread_join(thread2,(void**)&oddresult);

The proper way to do retrieve the result however would be to pass the addresses of actual void * variables and converting them:

int evenresult, oddresult;
void *result;
pthread_join(thread, &result);
evensum = (intptr_t)result;
...
pthread_join(thread2, &result);
oddsum = (intptr_t)result;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

Create all the thread and then join them like below:-

pthread_create(&thread1,0,&evensum,(void*)obj);
pthread_create(&thread2,0,&oddsum,(void*)obj);

pthread_join(&thread1,(void**)evensum);
pthread_join(&thread2,(void**)oddsum);
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17