The use of Pthread_create
int Pthread_create (pthread_t*, const pthread_attr_t*, void* (*) (void*), void*)
To make the g++ compile, the following methods are:
C + + prohibits assigning void pointers to other pointers at random.
So you get an error when you convert the entry of the void thread (void) function to void*, and then you call Pthread_create as a parameter, because the pthread_create argument should be a pointer like void* fun (void*) A pointer to a function.
You can modify void thread (void) to void* thread (void*), and then remove the (void*) cast at the time of the call, eliminating the error.
Cases:
void* Thread (void*) {
int i;
for (int i=0; i<3; i++) {
cout << ' This is a thread ' << Endl;
}
}
int main (int arg, char** argv) {
pthread_t id;
int I, ret;
ret = pthread_create (&id, NULL, thread, NULL);
if (ret!= 0) {
cout << "Create thread error!" << Endl;
Exit (1);
}
cout << "This is the main process" << Endl;
Pthread_join (ID, NULL);
return (0);
}
Change to GCC
void thread (void) {
int i;
for (int i=0; i<3; i++) {
cout << ' This is a thread ' << Endl;
}
}
int main (int arg, char** argv) {
pthread_t id;
int I, ret;
ret = pthread_create (&id, NULL, (void *) thread, NULL);
if (ret!= 0) {
cout << "Create thread error!" << Endl;
Exit (1);
}
cout << "This is the main process" << Endl;
Pthread_join (ID, NULL);
return (0);
}