多线程之pthread_create创建线程

pthreads定义了一套C程序语言类型、函数、与常量。以pthread.h和一个线程库实现。

数据类型:

pthread_t:线程句柄

pthread_attr_t:线程属性

线程操作函数:

pthread_create():创建一个线程

pthread_exit():终止当前线程

pthread_cancel():中断另外一个线程的运行

pthread_join():阻塞当前的线程,直到另外一个线程运行结束

pthread_attr_init():初始化线程的属性

pthread_attr_setdetachstate():设置脱离状态的属性

pthread_attr_getdetachstate():获取脱离状态的属性

pthread_attr_destroy():删除线程的属性

pthread_kill():向线程发送一个信号

.....

 

创建线程:

int pthread_create(pthread_t *thread, const pthread_attr_t  *attr, void *(*start_rtn)  (void*),void  *arg);

 

*thread:线程id (指向线程标识符的指针)

*attr:线程属性(通常为空)

*start_rtn:线程要执行的函数(线程运行函数的起始地址)

*arg:start_rtn的参数

返回值为零才算成功,其它表示错误

编译时需要加上 -lpthread

 

示例一:

#include <stdio.h>

#include <pthread.h>

 

void *pthread_1(void)      //线程1执行的函数

{

         inti;

         for(i=1; i < 5; i++){

                   printf("pthread1.\n");

                   sleep(1);

         }

return (void *)0;

}

 

void *pthread_2(void)      //线程2执行的函数

{

         inti;

         for(i=1;i < 5; i++){

                   printf("pthread2.\n");

                   sleep(1);

         }

return (void *)0;

}

 

int main(int argc, char argv[])

{

         intret = 0;

         pthread_tpthid1, pthid2;        //线程的id

         ret= pthread_create(&pthid1,NULL, (void *)pthread_1, NULL);

         if(ret)       // 0则创建失败

         {

                   perror("createthread 1 failed.\n");

                   return1;

         }

         ret= pthread_create(&pthid2, NULL, (void *)pthread_2, NULL);

         if(ret)

         {

                   perror("createthread 2 failed.\n");

                   return1;

         }

 

         pthread_join(pthid1,NULL);    //等待pthid1,否则不会等待,继续往下执行

         pthread_join(pthid2,NULL);    //如无,遇到下面的return会结束进程

         return0;

}

 

示例二:传递参数,初始化

/*

 *pthread_init.c

 *

 * Created on: Jan 20, 2013

 *     Author: linuxdba@qq.com

 */

 

#include <stdio.h>

#include <pthread.h>

 

void *create(void *arg)

{

         int *num;

        num = (int*)arg;

         printf("theinput number is: %d.\n", *num);

         return(void *)0;

}

 

 

int main(int argc, char argv[])

{

         intret;

         intt;

         printf("Inputone number:");

         scanf("%d",&t);

 

         int*attr = &t;

         pthread_tpthid;

         ret= pthread_create(&pthid, NULL, (void *)create, (void *)attr);

         if(ret)

         {

                   perror("createthread failed.\n");

                   return1;

         }

         pthread_join(pthid,NULL);

         return0;

}

 

示例三:共享数据

/*

 *pthread_share.c

 *

 * Created on: Jan 20, 2013

 *     Author: linuxdba@qq.com

 */

 

#include <stdio.h>

#include <pthread.h>

 

static int i = 3;

void *pthread_1(void)

{

         printf("thread1, i is: %d.\n", i);

         return(void *)0;

}

 

void *pthread_2(void)

{

         printf("thread2, i is: %d.\n", i);

         return(void *)0;

}

 

int main(int argc, char argv[])

{

         intret = 0;

         inti = 5;

         pthread_tpthid1, pthid2;

         printf("mainthread, i is: %d.\n", i);

         ret= pthread_create(&pthid1, NULL, (void *)pthread_1, NULL);

         if(ret)

         {

                   printf("createthread 1 failed.\n");

                   return1;

         }

         ret= pthread_create(&pthid2, NULL, (void *)pthread_2, NULL);

         if(ret)

         {

                   printf("createthread 2 failed.\n");

                   return1;

         }

         pthread_join(pthid1,NULL);

         pthread_join(pthid2,NULL);

         return0;

}

此段程序之中两个线程是对等的,不存在先后顺序;

i 为全局变量,main函数内部为局部的变量。

 

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_create.3.html

http://baike.baidu.com/view/974776.htm

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值