I'm writing a program that will simulate a randomized race between runners who are climbing up a mountain. I'm struggling with a part of my task where using the pthread_create() function, I am to create one thread for each runner in the race and save the thread pointer in the runner’s entity structure. The function that each runner thread will execute is the void* goRunner(void*) function that will be implemented later. The parameter to the goRunner() function will be the RunnerType pointer that corresponds to that runner.
As a note, while there are only 2 runner objects declared and initialized in my program so far/added to my runners array, my code should be implemented to support any number of runners.
So I made an attempt in main.c and I keep getting the error when I compile:
Code:
control.c: In function ‘launch’:
control.c:33:52: error: expected expression before ‘void’
pthread_create(&race->runners[i], NULL, goRunner(void RunnerType*), " ");
^~~~
control.c:33:18: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
pthread_create(&race->runners[i], NULL, goRunner(void RunnerType*), " ");
^
I'm not quite sure how to have a RunnerType pointer in the goRunner() parameter like it asks if the parameter type of the function is void*. Additionally, I don't really understand what the task is saying when it says to 'save the thread pointer in the runner’s entity structure.'
I noticed there is a data member pthread_t thr in the EntityType definition and I feel like I should be using it but I don't quite know what to do with it. I would guess thr replaces race->runners[i] in pthread_create() but this doesn't really make sense to me as I'm supposed to be creating a thread for each runner instead of just one thing?
main.c
Code:
int main(){
race = malloc(sizeof(RaceInfoType));
race->numRunners = 0;
init(race);
int i;
for(i = 0; i < race->numRunners; ++i){
pthread_create(&race->runners[i], NULL, goRunner(RunnerType*), " ");
}
}
void init(RaceInfoType* r){
addRunner(r, "Sarah", "S", 12, 40, 70, 0);
addRunner(r, "Henry", "H", 16, 25, 70, 0);
}
defs.h
Code:
typedef struct {
pthread_t thr;
char avatar[MAX_STR];
int currPos;
int path;
} EntityType;
typedef struct {
EntityType ent;
char name[MAX_STR];
int health;
int dead;
} RunnerType;
typedef struct {
int numRunners;
RunnerType *runners[MAX_RUNNERS];
char winner[MAX_STR]; //name of declared winner
int statusRow;
sem_t mutex;
} RaceInfoType;
void *goRunner(void*);
void init(RaceInfoType*);
RaceInfoType *race;