There are 2 structures (2 dynamic arrays), each with its own data set. There was a need to transfer one of the functions to the stream using pthread_create (the change function).
I can not understand how to transfer references to these 2 structures to this function (in the stream) so that I can perform some operations on these structures and with these changes I can continue to work in the main program?
Thank.
Sample program.
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #include <string.h> #include <inttypes.h> struct strc { char str[60]; int uid; struct strc *prev; }; struct param { struct stack *elma; struct stack *elmb; }; struct stack *add(struct stack **base, char *line,int uid) { struct stack *element=(struct stack*)malloc(sizeof(struct stack)); element->prev=*base; strcpy(element->data,line); element->uid=uid; return element; } int search(struct stack *base,char *str) { while (base!=NULL) {if (strcmp(base->data,str)==0) return base->uid; base=base->prev;} return 0; } void * change(void *arg) { struct param *data=arg; *data.elma=add(data.elma,"chips",5); *data.elmb=add(data.elmb,"volvo",2); } int main() { int uid; pthread_t thread; struct strc *elma=NULL; struct strc *elmb=NULL; struct param *arg; arg->elma=elma; arg->elmb=elmb; if (pthread_create(&thread, NULL, change, &arg) != 0) {return -1;} uid=search(elma,"chips"); printf("%i",uid); uid=search(elmb,"volvo"); printf("%i",uid); }
search()
is called before the end ofchange()
. Need synchronization of threads. For example, you can call pthread_join () - avp