0
votes

I am writing a small simple program that implements a cyclic executive schedule using pthreads. I first wrote the program without pthreads and am now trying to correctly pass parameters into pthread_create. I know that the args are:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

but sending the threads to ttable(the schedule) is giving me difficulty. Now that I am using threads I have added the void* x parameter even though I am not using it (is this parameter required?) I have tried leaving the schedule with no parameters and passing NULL in as the last arg of pthread_create as well. My error is currently: error: invalid conversion from 'void ( * )( )’ to ‘void ( * )(void*)’ [-fpermissive] }; ^

Here is the code:

#include <ctype.h>
#include <unistd.h>
#include <sys/times.h>
#include <pthread.h>

#define SLOTX   6
#define CYCLEX  4
#define SLOT_T  1000    //1 second slot time
#define NUM_THREADS 3

int tps;
int i;
int j;
int rc;
int cycle = 0;
int slot = 0;
struct tms n;

void one(void* x){
        printf("Task 1 running\n");
        sleep(1);
}

void two(void* x){
        printf("Task 2 running\n");
        sleep(1);
}

void three(void* x){
        printf("Task 3 running\n");
        sleep(1);
}

void burn(void* x){
        printf("Burn cycle\n");
        sleep(1);
}

void (*ttable[SLOTX][CYCLEX])(void* x) = {
        {one,   one,    two,    two},
        {three, three,  three,  three},
        {two,   two,    one,    one},
        {burn,  two,    two,    burn},
        {three, three,  three,  three},
        {one,   one,    two,    two}
};

main(){
        tps = sysconf(_SC_CLK_TCK);
        pthread_t thread[NUM_THREADS];
        printf("clock ticks/sec = %d\n\n", tps);
        while(1){
                printf("Starting new hyperperiod\n");
                for(slot = 0; slot<SLOTX; slot++){
                        printf("Starting new frame\n");
                        for(cycle = 0; cycle<CYCLEX; cycle++){
                                for(i = 0; i<NUM_THREADS; i++){
                                        rc = pthread_create(&thread[i], NULL, (*ttable[slot][cycle]), *(void*)j);
                                }
                        }
                }
        }

}
2
Use void one(void*) instead of void one() etc or I think you need to change (void *x) to just ()Justin
changing (void* x) to () gives me invalid conversion from void (*)() to void()(void*) and changing void one()/two()/three() to void one(void*) gives me invalid conversion from void()(void) to void(*)()Anna
Actually, I was wrong - you need to include an argument name since it's a function definition and not a declaration. Use a dummy argument as R.. suggested.Justin

2 Answers

3
votes

Start functions for pthread_create must take exactly one argument of type void *. Yours take zero arguments. Fix them to take a dummy argument of type void * and everything should be fine.

0
votes

Use the following:

typedef void* (*ThreadFunc_t) (void *);
ThreadFunc_t ttable[SLOTX][CYCLEX] = {
    {one,   one,    two,    two},
    {three, three,  three,  three},
    {two,   two,    one,    one},
    {burn,  two,    two,    burn},
    {three, three,  three,  three},
    {one,   one,    two,    two}
};

...

rc = pthread_create(&thread[i], NULL, (ttable[slot][cycle]), (void*)i);

If you use pthread_ctreate, you must follow its interface: int pthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

I hope this may help you!