related codes
-sharpinclude <stdio.h>
-sharpinclude <stdlib.h>
-sharpinclude <pthread.h>
void *print_message_function( void *ptr );
main()
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
if(iret1)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret1);
exit(EXIT_FAILURE);
}
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
if(iret2)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",iret2);
exit(EXIT_FAILURE);
}
printf("pthread_create() for thread 1 returns: %d\n",iret1);
printf("pthread_create() for thread 2 returns: %d\n",iret2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(EXIT_SUCCESS);
}
void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
confused place
in the function pthread_create (pthread_t * thread, const pthread_attr_t * attr,void * (* start_routine) (void *), void * arg);
), where void * (* start_routine) (void *) is the parameter "function pointer"
, and according to the function void * print_message_function (void * ptr);) From a prototype point of view, this is a function that returns any pointer type, not a function pointer, and there is no return value in the implementation of this function. But it can be compiled using gcc without any exceptions. Does anyone know why? Or give a reference link?
the use of functions like void * print_message_function (void * ptr);) that I know is
-sharpinclude<stdio.h>
void *test_f(int *n);
int main(void)
{
int test_num = 32;
int *test_p = &test_num;
int *ret_v = test_f(test_p);
printf("%d \n",*ret_v);
return 0;
}
void *test_f(int *n)
{
*n = *n + 1;
return n;
}