首页 > 其他 > 详细

Function: pthread_create

时间:2015-11-27 01:04:22      阅读:264      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!