Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread_create with no arguments?

I want to create a thread with no function arguments but I keep getting errors that are seriously bugging me because I cant get something super simple to work right

Heres my code:

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

It gives the error:

main.cpp:11:50: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ [-fpermissive]
like image 365
Johnny Johnson Avatar asked Oct 18 '15 01:10

Johnny Johnson


1 Answers

Your function has to match the pthread one. Meaning it needs to take and return a void*. Use void* showart(void*); instead.

like image 171
ChiefTwoPencils Avatar answered Sep 27 '22 18:09

ChiefTwoPencils