Function:pthread_create
do what:To create a new thread
head file:
include <pthread.h>
prototype:
int pthread_create( pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg );
explain:
demo: ptcreate.c
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> void *myThread( void *arg) { printf("thread ran! \n"); /* terminate the thread */ pthread_exit(NULL); } int main() { int ret; pthread_t mythread; void *point_null; ret = pthread_create( &mythread, NULL, myThread, NULL ); if (ret != 0) { printf( "Can‘t create pthread (%s)\n", strerror(errno) ); exit(-1); } pthread_join(mythread, &point_null); //wait for thread to terminate itself return 0; }
compile command:
gcc ptcreate.c -o ptcreate -lpthread
because function pthread is not the default library of linux system,
(在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库)
时间: 2024-09-29 00:15:47