c - Threading issue when calling same function -


i trying create 4 instances of given function having trouble working out how function called knows thread has called it.

this in header file:

// gpio pins stored within structs, each sonic range finder. typedef struct sonicpins {     // front left pins.     int trig1;     int echo1;     // front right pins.     int trig2;     int echo2;     // rear left pins.     int trig3;     int echo3;     // rear right pins.     int trig4;     int echo4; } args;  void* setup(void *pinsptr); extern int threadfunc(); 

this within c file.

int threadfunc() {     struct sonicpins * pins;     pthread_create(&pt1, null, setup, (void*) pins);     pthread_create(&pt2, null, setup, (void*) pins);     pthread_create(&pt3, null, setup, (void*) pins);     pthread_create(&pt4, null, setup, (void*) pins);     return 1; } 

the duty of snippet below set pin value , run operations manage sensor. each sensor has own echo , trigger values integers.

void* setup(void *pinsptr) {     struct sonicpins *ptr = pinsptr;     int trig = 0, echo = 0;      printf("thread id %d\n", pt1);     if (pt1 == 1993737328) {         trig = ptr->trig1;         echo = ptr->echo1;     } else if (pt2 == 1986323568) {         trig = ptr->trig2;         echo = ptr->trig2;     } else if (pt3 == 1977164912) {         trig = ptr->trig3;         echo = ptr->trig3;     } else if (pt4 == 4) {         trig = ptr->trig4;         echo = ptr->echo4;     }     …other work… } 

i new c , did forget thread id isn't same, i'm not sure can use base handling on. can suggest something?

what need array of structs, each struct has 1 echo , trigger value. pass different array entry each thread, each thread knows own echo , trigger values.

#include <stdio.h> #include <pthread.h>  typedef struct sonicpins {     int trig;     int echo; } sonicpins;  void *threadfunc( void *args ) {     sonicpins *pins = args;     printf( "trig=%d echo=%d\n", pins->trig, pins->echo );     return null; }  int main( void ) {     pthread_t threadid[4];     sonicpins pinsarray[4] = { { 1, 2 }, { 4, 8 }, { 16, 32 }, { 64, 128 } };      ( int = 0; < 4; i++ )     {         if ( pthread_create( &threadid[i], null, threadfunc, &pinsarray[i] ) != 0 )             fprintf( stderr, "pthread_create failed: %d\n", );     }      ( int = 0; < 4; i++ )         pthread_join( threadid[i], null ); } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -