-1

I got to use threads to solve a given problem, the only thing I need to do is make somehow that my threads use a function called launchR, I tested my function using no threads and runs ok, but whenever I try messing with threads I get segmentation fault and I cannot find the reason. This is how I handle my threads:

pthread_t threads[n];
    int rc, t;
    int *tid[n];

    int bpt  = b/n,
        macc = b%n,
        adv  = 0;
    for (t=0; t < n; t++) {
       tid[t] = (int *)malloc(sizeof(int));
       *tid[t]  = t;

       printf("Creando el Hilo %d\n",t);
       if (macc == 0 ) {
           rc = pthread_create(&threads[t],NULL,launchR(objarr,bombs,adv,obj,bpt), (void *)tid[t]);
           printf("\n Cree el Hilo THID: %d \n",*(tid[t]));
           if (rc) {
              printf("Error, %d\n",rc);
              exit(-1);
            }
           adv=adv+bpt;
       }
       else if (macc > 0 ) {
           rc = pthread_create(&threads[t],NULL,launchR(objarr,bombs,adv,obj,bpt+1), (void *)tid[t]);
           printf("\n Cree el Hilo THID: %d \n",*(tid[t]));
           if (rc) {
              printf("Error, %d\n",rc);
              exit(-1);
           }

           adv=adv+bpt+1;
           printf("%d\n", adv);
       }
       macc--;
    }

    for (t=0; t < n; t++) {
        pthread_join(threads[t],NULL);
    }

I would appreciate any advice you could give me.

Edit: here's the code for the function launch, it's based on another function.

void launch ( int obj[][5] , int bombs[][4] , int bomb, int objs ) {
for (int i = 0; i < objs; ++i) {
    if ( obj[i][0] <= (bombs[bomb][0] + bombs[bomb][2]) && 
        obj[i][0] >= (bombs[bomb][0] - bombs[bomb][2]) &&
        obj[i][1] <= (bombs[bomb][1] + bombs[bomb][2]) && 
        obj[i][1] >= (bombs[bomb][1] - bombs[bomb][2])){

        if (obj[i][3] == 1) {
            obj[i][2] = obj[i][2] - bombs[bomb][2];
            obj[i][4] = 1;
        }

        else if (obj[i][3] == -1){
            obj[i][2] = obj[i][2] + bombs[bomb][2];
            obj[i][4] = 1;
        }
    }
}
}




void *launchR(int obj[][5] , int bombs[][4] , int ibomb, int objs, int cant){

for (int i = ibomb; i <= cant; i++) {
    launch(obj,bombs,i,objs);
}

}

Edit 2: Here is my output:

entro en threads
Creando el Hilo 0

 Cree el Hilo THID: 0 
2
Creando el Hilo 1
[1]    19677 segmentation fault  ./batalla -t -n 3 prueba
Stieg
  • 137
  • 10
  • 1
    What does your compiler say about `rc = pthread_create(&threads[t],NULL,launchR(objarr,bombs,adv,obj,bpt), (void *)tid[t]);` (which is almost certainly incorrect, and the compiler is *probably* obligated to warn you). – EOF Nov 03 '16 at 22:30
  • It does not say anything when I compile it, not even a warning. Why would it be incorrect? – Stieg Nov 03 '16 at 22:34
  • http://stackoverflow.com/questions/1352749/multiple-arguments-to-function-called-by-pthread-create – yano Nov 03 '16 at 22:36
  • Because I find it unlikely for `launchR()` to return a `void *(*)(void*)`. If my suspicion is correct (*and* you properly `#include`d ``) then the compiler must issue a diagnostic message for passing an incorrect type to `pthread_create()`. – EOF Nov 03 '16 at 22:36
  • My code is based on this one http://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux , and there's the launchR code, I recently edited my question for someone who asked and deleted his comment. – Stieg Nov 03 '16 at 22:40
  • @Manuel: Well, with the prototype of `launchR()` visible my suspicion is confirmed. If your compiler does not issue a diagnostic message, it is broken and you should get a compiler that actually implements C correctly. – EOF Nov 03 '16 at 22:45
  • I'm using gcc, I doubt it's broken. – Stieg Nov 03 '16 at 22:46
  • Which version of gcc are you using, and what compiler flags are you passing to it? – EOF Nov 03 '16 at 22:51
  • gcc version 4.9.2 (Debian 4.9.2-10) `CCFLAGS = -g -Wall -std=c99 -pthread` – Stieg Nov 03 '16 at 22:56
  • You're passing arguments to `launchR` in `pthread_create` incorrectly. The arguments to `launchR` go in the last argument of `pthread_create`. If you have multiple arguments (you do), you'll need to pack all these up in a `struct` and pass that to `lanuchR` – yano Nov 03 '16 at 22:59
  • @yano Thanks mate, I'll try that. – Stieg Nov 03 '16 at 23:02
  • @Manuel Unfortunately gcc doesn't honor its obligation to issue a diagnostic message about this unless you pass `-Wpedantic`. – EOF Nov 03 '16 at 23:03

1 Answers1

0

The function declaration is void launch ( int obj[][5] , int bombs[][4] , int bomb, int objs ); hence the argument in the pthread_create call should be the address of the function launch. pthread_create should be :

pthread_create(&threads[t],NULL,(void *)&launchR(objarr,bombs,adv,obj,bpt+1), (void *)tid[t]); 
Darshan b
  • 157
  • 7