简体   繁体   English

将参数传递给pthread_create-从void(*)()到void(*)(void *)的无效转换

[英]Passing arguments to pthread_create - invalid conversion from void(*)() to void(*)(void*)

I am writing a small simple program that implements a cyclic executive schedule using pthreads. 我正在编写一个小的简单程序,该程序使用pthreads实现循环执行计划。 I first wrote the program without pthreads and am now trying to correctly pass parameters into pthread_create. 我首先编写了没有pthreads的程序,现在尝试将参数正确地传递到pthread_create中。 I know that the args are: 我知道args是:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 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. 但是将线程发送到ttable(日程表)给我带来了困难。 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. 现在,我正在使用线程,即使我没有使用它,我也添加了void * x参数(是否需要此参数?)我尝试不使用任何参数的调度,并将NULL作为pthread_create的最后一个参数传入。 My error is currently: error: invalid conversion from 'void ( * )( )' to 'void ( * )(void*)' [-fpermissive] }; 我目前的错误是:错误:从'void(*)()'到'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);
                                }
                        }
                }
        }

}

Start functions for pthread_create must take exactly one argument of type void * . pthread_create启动函数必须恰好带有一个类型为void *参数。 Yours take zero arguments. 您的参数为零。 Fix them to take a dummy argument of type void * and everything should be fine. 修复它们,使其采用类型为void *的虚拟参数,一切都应该正常。

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); 如果使用pthread_ctreate,则必须遵循其接口: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! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无效转换从void *到void(*)(void *)[ - fpermissive] - invalid conversion from void* to void(*)(void*)[-fpermissive] pthread_create int而不是void - pthread_create int instead of void 错误:从&#39;void *&#39;到&#39;arguments *&#39;的无效转换[-fpermissive] - error: invalid conversion from ‘void*’ to ‘arguments*’ [-fpermissive] 错误:从'void(*)()'到'void(*)()'的转换无效 - 什么? - error: invalid conversion from 'void (*)()' to 'void (*)()' — what? C-从&#39;void *&#39;到&#39;void(*)()&#39;的无效转换 - C - invalid conversion from ‘void*’ to ‘void (*)()’ 在OpenCL clCreateContext中从void(*)(...)到void(*)(...)的转换无效 - Invalid conversion from void (*) (…) to void (*) (…) in OpenCL clCreateContext 由于无效指针和整数转换错误,pthread_create无法正常工作 - pthread_create does not work due to void pointer and integer conversion error 错误:从&#39;void *&#39;到&#39;int(*)(const void *,const void *)&#39;的无效转换 - error: invalid conversion from 'void*' to 'int (*)(const void*, const void*)' 调用pthread_create错误-&#39;void&#39;之前的预期主表达式 - Calling pthread_create error - expected primary expression before 'void' 在pthread_create()中,将int类型转换为void *是什么意思? - What does int type convert to void* mean in pthread_create()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM