Angel.King.47 Angel.King.47 - 1 year ago 157
C++ Question

Simple pthread! C++

I have no idea why this doesn't work

#include <iostream>
#include <pthread.h>
using namespace std;

void *print_message(){

cout << "Threading\n";

int main() {

pthread_t t1;

pthread_create(&t1, NULL, &print_message, NULL);
cout << "Hello";

return 0;

The error:

[Description, Resource, Path, Location, Type] initializing argument 3
of 'int pthread_create(pthread_t*, const pthread_attr_t*, void*
(*)(void*), void*)' threading.cpp threading/src line 24 C/C++

Answer Source

You should declare the thread main as:

void* print_message(void*) // takes one parameter, unnamed if you aren't using it
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download