kek kek - 1 year ago 99
C Question

Can't compile pthread scheduling API

So in my lecture my professor gave us some code we need to use in our next assignment, it's titled the Pthread scheduling API and this is the code:

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5

int main(int argc, char *argv[])

int i;
pthread_t tid[NUM_THREADS];
pthread_attr_t attr;
/*get default attributes */
/* set scheduling algorithm to process or system */
/* set scheduling policy -FIFO, RT or OTHERS*/
/* create the threads */
for (i=0;i<NUM_THREADS;i++)
/* join each thread*/
for (i=0;i<NUM_THREADS;i++)

void *runner(void *param){
printf("I am a thread\n");

however when I compile this it tells me runner is not declared when it gets used in pthread_create. Is this supposed to be able to run? am I doing something wrong? if it's not supposed to run how do I go about using this API? I've looked around on the internet and it seems a lot of lectures teach this API but I can't find out how to use it, what I'm trying to do with this API is to just create a few concurrent threads with one of them being the parent and print stuff with those threads

Answer Source

put you function definition before the main :

void *runner(void *param);

Otherwise , at compile time runner is not defined because you are defining it at the end.

Putting the definition on top and the implementation on the bottom will solve the problem. (you could also just put your function implementation on top of the main)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download