线程的创建、终止、等待基本操作

1、 pthread_create创建线程

#include<pthread.h>

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

  tidp:线程id,创建时分配id给Tidp

  attr:线程属性,通常为空NULL

  start_rtn:线程要执行的函数

  arg:   start_rtn的参数

  返回值>0表示创建失败。

2、pthread_exit终止线程

如果进程中任何一个线程中调用exit或_exit,那么整个进程都会终止。

线程的正常退出方式有:

A、 线程从启动例程中返回,如:return n;或者return (void *)n;此句表示线程函数的返回值为void型。

B、 线程可以被其他线程终止pthread_cancel(pthread_t tid)。

C、 线程自己调用pthread_exit函数

3、pthread_join线程等待

#include<pthread.h>

Int pthread_join(pthread_t tid,void **rval_ptr)

功能:阻塞线程等待,直到指定的线程终止。即等待tid的线程结束,此线程的返回值赋给rval_ptr。

Tid:等待退出的线程id

Rval_ptr:线程退出的返回值的指针

 

4、 pthread_self线程标识(id)的获取

#include<pthread.h>

Pthread_t pthread_self(void)

功能:获取调用线程的thread identifier

 

 

注意:thread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。

如:gcc filename.c lpthread o filename

否则将出现以下错误:
undefined reference to 'pthread_create'

undefined reference to 'pthread_join'

 

实例代码: 

#include<stdio.h>

#include<stdlib.h>

#include<stddef.h>

#include<unistd.h>

#include<pthread.h>

 

int a=2;

 

struct menber

{

         int a;

         char *s;

}

 

thread1()

{

         int i;

         for(i=0;i<5;i++)

         {

                   printf("this is the 1st pthread\n");

                   sleep(1);

         }

}

 

thread2()

{

         int i;

         for(i=0;i<5;i++)

         {

                   printf("this is the 2st pthread\n");

                   sleep(1);

         }

}

 

void *create_int(void *arg)

{

         int *num;

         num=(int *)arg;//voidxing指针强制转换为int型指针

         printf("this is the 3st pthreas :create parameter is %d \n",*num);

         return (void *)0;

}

 

void *create_chars(void *arg)

{

       

        //num=(int *)arg;//voidxing指针强制转换为int型指针

        printf("this is the 4st pthread:create parameter is %s \n",(char *)arg);

        return (void *)0;

}

 

void *create_share(void *arg)

{

         printf("this is the create_share pthread...\n");

         printf("a=%d \n",a);

         a++;

         return (void *)0;

}

 

void *create_struct_6(void *arg)

{

         struct menber *temp;

         temp=(struct menber *)arg;

         printf("menber->a=%d\n",(*temp).a);

         printf("menber->s=%s\n",temp->s);

         return (void *)0;

}

 

void *create_exit_7(void *arg)

{

       

        printf("this is the create_exit_7  test\n");

//        return (void *)0;//线程从启动例程中返回

//      pthread_exit((void *)8);//线程自己调用函数退出

         sleep(3);

         exit(0);//线程中调用exit_exit,那么整个进程都会终止

}

 

 

 

int main()

{

         int ret;

         char *s="hello world!\n";

         pthread_t id1,id2,id3,id4,id5,id6,id7;

         int test=5;

         int *arg=&test;

         void *temp_exit;

//      int a=10;

         a=12;

        

         struct menber *b;

         b=(struct menber *)malloc(sizeof(struct menber));

         (*b).a=5; //或写成b->a=5;

         (*b).s="zhang";

 

         /*创建线程1*/

         ret=pthread_create(&id1,NULL,(void*)thread1,NULL);

        if(ret!=0)

        {       

                            printf("thread_send error\n");

                            exit(1);

         }

         /*创建线程2*/

 

         ret=pthread_create(&id2,NULL,(void*)thread2,NULL);

         if(ret)

         {

                   printf("create thread error\n");

         }

 

 

      

         /*创建线程3 create_int,传递一个整型数*/

 

        ret=pthread_create(&id3,NULL,(void*)create_int,(void *)arg);//传递时强制转换成void型指针

        if(ret)

        {

                printf("create thread error\n");

        }

 

          /*创建线程4 create_chars,传递字符串*/

 

        ret=pthread_create(&id4,NULL,(void*)create_chars,(void *)s);//传递时强制转换成void型指针

        if(ret)

        {

                printf("create thread error\n");

        }

 

         /*创建线程5 create_share,实现线程对变量共享访问*/

 

        ret=pthread_create(&id5,NULL,(void*)create_share,(void *)s);//传递时强制转换成void型指针

        if(ret)

        {

                printf("create thread error\n");

        }

 

         /*创建线程6 create_struct_6,实现结构体的传递*/

 

        ret=pthread_create(&id6,NULL,(void*)create_struct_6,(void *)b);//传递时强制转换成void型指

        if(ret)

        {

                printf("create thread error\n");

        }

        

         /*创建线程7 create_exit_7,实现线程的退出*/

 

        ret=pthread_create(&id7,NULL,(void*)create_exit_7,NULL);//传递时强制转换成void>型指针

        if(ret)

        {

                printf("create thread error\n");

        }

 

 

 

         pthread_join(id1, NULL);//等待线程1 结束

         pthread_join(id2, NULL);

         ret=pthread_join(id7, &temp_exit);

 

         if(ret)

         {

                   printf("thread 7 is not exit ... \n");

                   return -2;

         }

/*注意观察线程create_exit_7中不同的返回方式,进程的执行情况*/

 

         printf("thread exit code %d \n",(int)temp_exit);

         printf("int main :a=%d \n",a);

 

/*     while(1)

        {

        printf("this is the main-thread\n");

        sleep(1);

        }

*/

         return 0;

}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值