Answers for "how to pass an array value to a pthread in c"

C
3

how to pass an array value to a pthread in c

void* my_Func(void *received_arr_Val){
	
	int single_val = (int *)received_arr_Val;
    printf("Value: %d\n", single_val);
	//Now use single_val as you wish
}
//In main:
	int values[n];
    pthread_create(&thread, NULL, my_Func, values[i]);
	//i is the index number of the array value you want to send
	//n is the total number of indexes you want (array size)

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Posted by: Guest on March-02-2020
1

how to pass an array value to a pthread in c

//Runnable example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void* my_Func(void *received_arr_Val){
	
	int single_val = (int *)received_arr_Val;
    printf("Value: %d\n", single_val);
	//Now use single_val as you wish
}
int main(){
	int values[5];
	
	printf("\nEnter 5 numbers:\n");
	for (int i=0; i<5; i++){
		scanf("%d", &values[i]);
	}
	pthread_t tid[5];
	for(int i=0; i<5; i++){
		pthread_create(&(tid[i]), NULL, my_Func, values[i]);
	}
	for (int i=0; i<5; i++){	
		pthread_join(tid[i], NULL);
	}
}
//To run: $ gcc [C FILE].c -lpthread -lrt
// $ ./a.out

//Grepper profile: https://www.codegrepper.com/app/profile.php?id=9192
Posted by: Guest on March-02-2020

Code answers related to "how to pass an array value to a pthread in c"

Code answers related to "C"

Browse Popular Code Answers by Language