码迷,mamicode.com
首页 > 其他好文 > 详细

Function: pthread_create

时间:2015-11-27 01:07:04      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

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系统的默认库)

Function: pthread_create

标签:

原文地址:http://www.cnblogs.com/craigtao/p/4999392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!