c++ - pthread_create 没有参数?

标签 c++ c++11 pthreads posix

我想创建一个没有函数参数的线程,但我一直收到严重困扰我的错误,因为我无法让一些 super 简单的东西正常工作

这是我的代码:

#include<stdio.h>
#include<array>
#include<pthread.h>
#include<fstream>
#include<string>

void *showart(NULL);

int main(int argc,  char** argv){
    pthread_t thread1;
    pthread_create( &thread1, NULL, showart, NULL);
    getchar();
    return 0;
}

void *showart(NULL)
{
    std::string text;
    std::ifstream ifs("ascii");
    while(!ifs.eof()) 
    {
      std::getline(ifs,text);
      printf(text.c_str());
    }
}

它给出了错误:

main.cpp:11:50: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ [-fpermissive]

最佳答案

您的函数必须与 pthread 相匹配。这意味着它需要获取并返回一个 void*。请改用 void* showart(void*);

关于c++ - pthread_create 没有参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193333/

相关文章:

c++ - 具有未定义第二维的二维数组

c++ - 为什么 std::future::wait_for 不等待正确的持续时间?

c++ - 哪种代码被认为是异常安全的?

android - 线程同步@Native Android

c++ - 将字符串转换为 double - 精度丢失

c++ - 预处理器指令是否仅适用于写入它的文件?

c++ - 如何测量程序执行100次的时间?

C++11:为什么允许分配右值?

c++ - 使用 png++ 编写 png 图像

c - 变量失去值(value)