How to pass a struct value to a pthread in c?
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//Runnable example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct my_Struct{
int index;
int value;
};
void* my_Func(void *received_struct){
struct my_Struct *struct_ptr = (struct my_Struct*) received_struct;
printf("index: %d | value: %d \n", struct_ptr->index, struct_ptr->value);
//Now use 'struct_ptr->index', 'struct_ptr->value' as you wish
}
int main(){
struct my_Struct mystruct_1[5];
printf("\nEnter 5 numbers:\n");
for (int i=0; i<5; i++){
scanf("%d", &mystruct_1[i].value);
mystruct_1[i].index = i;
}
pthread_t tid[5];
for(int i=0; i<5; i++){
pthread_create(&(tid[i]), NULL, my_Func, &mystruct_1[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
Also in C:
- Title
- hentai clochette
- Category
- C
- Title
- C largest unsigned int
- Category
- C
- Title
- arduino vscode upload choosing sketch
- Category
- C
- Title
- how to get user input in c
- Category
- C
- Title
- c producer consumer pthread semaphore
- Category
- C
- Title
- c value set to zero __memmove_avx_unaligned_erms
- Category
- C
- Title
- how to store a user input with spaces in c
- Category
- C
- Title
- hostbuilder add environment variables
- Category
- C
- Title
- c concatenate strings
- Category
- C
- Title
- read from stdin c
- Category
- C
- Title
- Declaring Variables in C
- Category
- C
- Title
- xmlns='' was not expected
- Category
- C
- Title
- remove element queue
- Category
- C
- Title
- vbnet create and write on file
- Category
- C
- Title
- delete string function in c
- Category
- C
- Title
- doubly linked list c
- Category
- C
- Title
- arduino digital read
- Category
- C
- Title
- sdl bouton
- Category
- C
- Title
- error: dereferencing pointer to incomplete type
- Category
- C
- Title
- .\main.c:4:8: error: expected declaration specifiers or '...' before '\x6f726c64'
- Category
- C
- Title
- fa fa-facebook
- Category
- C
- Title
- docker pull command
- Category
- C
- Title
- simpy process return value
- Category
- C
- Title
- multiplication operator in c
- Category
- C
- Title
- PS1 modified lags
- Category
- C
- Title
- change no_turbo
- Category
- C
- Title
- c bit access union
- Category
- C
- Title
- debian install npm
- Category
- C
- Title
- passing 'const char *' to parameter of type 'char *' discards qualifiers
- Category
- C
- Title
- typedef in c
- Category
- C
- Title
- tar cmd
- Category
- C
- Title
- c string concatenation with int
- Category
- C
- Title
- find gcd iteratively
- Category
- C
- Title
- add border to image android
- Category
- C
- Title
- function pointer c
- Category
- C
- Title
- a enum data type in c with loop
- Category
- C
- Title
- java.lang.SecurityException: Permission denied (missing INTERNET permission?)
- Category
- C
- Title
- what is conio.h
- Category
- C
- Title
- mangoosejs
- Category
- C
- Title
- how to put quotes inside string c
- Category
- C
- Title
- c
- Category
- C
- Title
- for loop c
- Category
- C
- Title
- lelcetric fied
- Category
- C
- Title
- resto de division recursiva
- Category
- C
- Title
- a enum data type in c
- Category
- C
- Title
- factors using recursion
- Category
- C
- Title
- diferencia entre * y & en c
- Category
- C
- Title
- powershell list big files
- Category
- C
- Title
- vbl share price
- Category
- C
- Title
- print in c
- Category
- C
- Title
- how can i remove a specific item from an array
- Category
- C
- Title
- arduino digital input pins
- Category
- C
- Title
- uninstall elgg from hostgtor
- Category
- C
- Title
- how to go to top of file in vim
- Category
- C
- Title
- scanf integer
- Category
- C
- Title
- read a binary file c
- Category
- C
- Title
- disable gnu++11 option
- Category
- C
- Title
- ‘uint64_t’ was not declared in this scope
- Category
- C
- Title
- ModuleNotFoundError: No module named 'cv2'
- Category
- C
- Title
- hello world
- Category
- C
- Title
- declare integer c
- Category
- C
- Title
- casting in c
- Category
- C
- Title
- prime number
- Category
- C
- Title
- c struct
- Category
- C
- Title
- windeployqt example
- Category
- C
- Title
- mongodb delete all documents
- Category
- C
- Title
- restapi
- Category
- C
- Title
- how to pass an array value to a pthread in c
- Category
- C
- Title
- get_session` is not available when using TensorFlow 2.0.
- Category
- C
- Title
- c check if array is empty
- Category
- C
- Title
- sbatch array set max jobs at once
- Category
- C
- Title
- set timezone in debian terminal
- Category
- C
- Title
- declaring a volatile in c
- Category
- C
- Title
- Couldn't create temporary file to work with
- Category
- C
- Title
- c matrix sintax
- Category
- C
- Title
- reverse function in c
- Category
- C
- Title
- what are the causes of memory leaks in c
- Category
- C
- Title
- write in file in c
- Category
- C
- Title
- c radians
- Category
- C
- Title
- sustituir un dígito por otro
- Category
- C
- Title
- entete
- Category
- C
- Title
- link whatsapp to website
- Category
- C
- Title
- primo
- Category
- C
- Title
- c program to count duplicates in an array
- Category
- C
- Title
- double return type in c
- Category
- C
- Title
- delete docker image repository none
- Category
- C
- Title
- allocate memory c
- Category
- C
- Title
- excel cell blank or zero
- Category
- C
- Title
- Switch Mode C Programming
- Category
- C
- Title
- c vs python
- Category
- C
- Title
- c print hello world
- Category
- C
- Title
- stack implementation
- Category
- C
- Title
- c check if char is number
- Category
- C
- Title
- how to sleep in c
- Category
- C
- Title
- 1 212 32123 4321234 543212345 in c
- Category
- C
- Title
- how to check where the last char is in a string c
- Category
- C
- Title
- write a program to ask the user 8 integer numbers. your program will then move all negative elements of your array to the end of the array without changing the order of positive elements and negative elements
- Category
- C
- Title
- vector aleatorio sin repetir
- Category
- C
- Title
- tmux how to kill all sessions
- Category
- C
- Title
- diagonales
- Category
- C