Pthread_create with no arguments?

I want to create a stream with no function arguments, but I keep getting errors that seriously eavesdrop on me, because I can't get something super simple to work correctly

Here is 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 an error:

main.cpp:11:50: error: invalid conversion fromvoid*’ to ‘void* (*)(void*)’ [-fpermissive]
+4
source share
2 answers

Your function should match pthread one. This means that you need to take and return void*. Use instead void* showart(void*);.

+3
source

Both the declaration and definition for your stream function is incorrect. You can use NULLthe call, but the type of the parameter required for the declaration / definition void *.

, - :

void *showart(void *);              // declaration
void *showart(void *unused) { ... } // definition

, :

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

void *showart (void *);

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

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

, , , , , pthread_create(), main(), ..

0

Source: https://habr.com/ru/post/1612098/


All Articles