当前位置: 首页 > 知识库问答 >
问题:

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

康烨伟
2023-03-14

我正在编写一个小的简单程序,它使用pthreads实现了一个循环执行时间表。我首先编写了没有pthreads的程序,现在试图将参数正确地传递到pthread_create中。我知道args是:

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

#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);
                                }
                        }
                }
        }

}

共有1个答案

孔光赫
2023-03-14

pthread_create的Start函数必须只有一个void*类型的参数。你的零论点。将它们修复为使用void*类型的虚拟参数,一切都应该很好。

 类似资料:
  • 我必须将普通的C .h文件集成到我的C项目(Qt GUI)中。为此,我必须避免一些带有棘手替换的函数指针。您可以在下面看到的代码段之前在MinGW 5.3.0 C中编译没有任何问题。但是当我尝试使用更多新的编译器(C为7.3.0)时,我收到一个错误“从'int'到'void(void)(void)'的无效转换”,但它仍然编译没有问题与相同的7.3.0 for C。 问:编译器给我的宏表达式错误是

  • 我一直在按照创建 POSIX 线程的说明进行操作,并密切关注他们的示例 Pthread 创建和终止。 根据他们的示例,他们将一个整数值传递给,这将导致类似于以下内容的调用: 我要运行的代码的草图是: 这是在一个作业的上下文中,老师希望我们在Linux中使用gcc,所以我使用Windows子系统来Linux这些编译参数: 运行上述程序将输出“线程值:42”。 我的 C 指针体验已经过去了几年,但只是

  • 描述 (Description) C库函数void free(void *ptr)释放先前通过调用calloc,malloc或realloc分配的内存。 声明 (Declaration) 以下是free()函数的声明。 void free(void *ptr) 参数 (Parameters) ptr - 这是指向先前分配有要释放的malloc,calloc或realloc的内存块的指针。 如果将

  • 字符串: 给我错误: Description资源路径位置类型从'void(attribute((stdcall))(GLenum,GLenum,GLuint,GLenum,GLsizei,const GLchar,GLvoid*){aka void(attribute((stdcall)))(unsigned int,unsigned int,unsigned int,unsigned int,u

  • 描述 (Description) C库函数void abort(void)中止程序执行并直接从调用位置输出。 声明 (Declaration) 以下是abort()函数的声明。 void abort(void) 参数 (Parameters) NA 返回值 (Return Value) 此函数不返回任何值。 例子 (Example) 以下示例显示了abort()函数的用法。 #include <

  • 描述 (Description) C库函数void *memmove(void *str1, const void *str2, size_t n) n字符从str2复制到str1 ,但对于重叠的内存块,memmove()比memcpy()更安全。 声明 (Declaration) 以下是memmove()函数的声明。 void *memmove(void *str1, const void *s