c - Passing structures in pthread -


i trying pass structure when creating thread not seem work correctly!

here structure:

struct analyse_data {     int verbose; //should 1 or 0 }; 

note verbose can 1 or 0 , nothing else.

here method being called (note can called multiple times method):

void dispatch(struct pcap_pkthdr *header, const unsigned char *packet,         int verbose) {      static bool thread_settings_initialised = false;      printf("verbose: %d\n", verbose); //prints 1 or 0      //only run first time dispatch method runs     if (thread_settings_initialised == false){         thread_settings_initialised = true;          //...              //set mutex appropriate variables remain thread safe         //...          //set attr threads "detached"         pthread_attr_init(&attr);         pthread_attr_setdetachstate(&attr, pthread_create_detached);          //set pthread_cond_init         //...     }      //put parameters struct can sent in thread     struct analyse_data data;     data.verbose = verbose;      //...      pthread_t tid;     printf("data.verbose: %d\n", data.verbose); //this prints 1 or 0     int rc = pthread_create( &tid, &attr, bar, (void *) &data);     if (rc) {         printf("error; return code pthread_create() %d\n", rc);         exit(-1);     }  } 

and method thats being called thread:

void analyse(void *thread_data) {     struct analyse_data *data;     data = (struct analyse_data *) thread_data;     int verbose = data->verbose;     printf("verbose =  %d\n", verbose ); //prints weird numbers -547845... } 

as can see comments, value of verbose changes when being used inside method. why? doing wrong?

many thanks!

update (thanks js1) updated code use pointer:

void dispatch(struct pcap_pkthdr *header, const unsigned char *packet,         int verbose) {      static bool thread_settings_initialised = false;      printf("verbose: %d\n", verbose); //prints 1 or 0      //...      //put parameters struct can sent in thread     struct analyse_data *data = malloc(sizeof(struct analyse_data)); //new     data->verbose = verbose;      //...      pthread_t tid;     printf("data.verbose: %d\n", data.verbose); //this prints 1 or 0     int rc = pthread_create( &tid, &attr, bar, (void *) data);     //... } 

but analyse() method outputs 0, when verbose 1!

you should not pass stack variable pthread_create. notice data local function dispatch , out of scope when dispatch returns. should instead either use malloc allocate data, or use static or global variable.

if use malloc method, this:

struct analyse_data *data = malloc(sizeof(struct analyse_data)); data->verbose = verbose;  int rc = pthread_create( &tid, &attr, bar, data); 

you must remember not call free on data dispatch. memory should "belong" thread, should call free on data thread when done using contents of data.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -