How to pass a struct value to a thread in c? code example

Example: How to pass a struct value to a pthread in c?

struct my_Struct{
	int val1, val2;
};
void* my_Func(void *received_struct){
  	//Creating a pointer to point to the received struct
	struct my_Struct *struct_ptr = (struct my_Struct*) received_struct;
    printf("Value 1: %d | Value 2: % \n", struct_ptr->val1, struct_ptr->val2);
	//Now use 'struct_ptr->val1', 'struct_ptr->val2' as you wish
}
//In main:
		struct my_Struct mystruct_1[n];
		pthread_create(&thread, NULL, my_Func, &mystruct_1[i]);
		//i is the index number of mystruct_1 you want to send
		//n is the total number of indexes you want

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192

Tags:

C Example