Thread: Creating thread for every element in array using pthread_create()

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    11

    Creating thread for every element in array using pthread_create()

    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;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,419
    > pthread_create(&race->runners[i], NULL, goRunner(RunnerType*), " ");
    You don't specify the type.

    pthread_create(&race->runners[i], NULL, goRunner, " ");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to compare element of array with other element
    By Djsarkar in forum C Programming
    Replies: 28
    Last Post: 08-04-2020, 03:59 PM
  2. Insert element after the last element in array in c
    By amaturequestion in forum C Programming
    Replies: 3
    Last Post: 04-09-2015, 08:29 AM
  3. problem with creating a thread
    By neeharikach in forum C Programming
    Replies: 1
    Last Post: 11-28-2010, 03:46 AM
  4. creating thread/sleep
    By it99 in forum C Programming
    Replies: 3
    Last Post: 06-17-2010, 11:04 PM
  5. Creating a thread in a class - C++
    By rag84dec in forum Windows Programming
    Replies: 1
    Last Post: 08-07-2009, 03:52 AM

Tags for this Thread