linux下c++ 多线程的应用示例

本文介绍了Linux下C++实现多线程的方法,包括`pthread_create`用于创建线程,`pthread_join`等待线程结束并回收资源,以及`pthread_exit`线程退出,还讲解了互斥锁和条件锁的概念及使用。通过两个示例程序`test1.cpp`和`test2.cpp`展示了无参和含参多线程的创建与管理。
摘要由CSDN通过智能技术生成
        ·线程创建

  函数原型:int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

  返回值:若是成功建立线程返回0,否则返回错误的编号。

  形式参数:pthread_t *restrict tidp要创建的线程的线程id指针;const pthread_attr_t *restrict attr创建线程时的线程属性;void* (start_rtn)(void)返回值是void类型的指针函数;void *restrict arg start_rtn的形参。

  ·线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。

  函数原型:int pthread_join( pthread_t thread, void **value_ptr);

  参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。

  ·线程退出

  函数原型:void pthread_exit(void *rval_ptr);

  ·获取当前线程id

  函数原型:pthread_t pthread_self(void);

  ·互斥锁

  创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。

  ·条件锁

  创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast S;等待pthread_cond_wait。 

    

 ·正确处理Linux平台下的线程结束问题

  在Linux平台下,当处理线程结束时需要注意的一个问题就是如何让一个线程善始善终,让其所占资源得到正确释放。在Linux平台默认情况下,虽然各个线程之间是相互独立的,一个线程的终止不会去通知或影响其他的线程。但是已经终止的线程的资源并不会随着线程的终止而得到释放,我们需要调用pthread_join() 来获得另一个线程的终止状态并且释放该线程所占的资源。

/test1.cpp/   无参多线程,主线程等待其他线程完成后运行

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

//int flag
void *thread(void *ptr)
{
    int i;
    for(i=0;i<3;i++)
    {
sleep(1);
printf("this is a pthread.\n");
    }

    //flag =1;
}

int main(void)
{
    pthread_t id;
    int ret;
    ret=pthread_create(&id,NULL,thread,NULL);
    if(ret!=0)
    {
printf("Create pthread error!\n");
exit(1);
    }
    
    //while(flag!=1) {}  
    /*

    for(i=0;i<3;i++)
    {
printf("this is the main process.\n");
sleep(1);
    }*/

    pthread_join(id,NULL);
    printf("this is the main process.\n");
    //flag=0;
    return(0);
}



/test2.cpp/   含一参多线程,主线程等待其他线程完成后运行

#include <stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>

void* thread(void *arg)
{
    int count=*(int *)arg;
    for(int i=0;i<count;i++)
    {
sleep(1);
printf("this is a pthread.\n");
    }
  
}


int main(void)
{
    pthread_t id;
    int ret;
    srand(int(time(0)));
    int count=rand()%10;

    ret=pthread_create(&id,NULL,thread,&count);
    if(ret!=0)
    {
printf("Create pthread error!\n");
exit(1);
    }
    
    //while(flag!=1) {} 
    /*
    for(i=0;i<3;i++)
    {
printf("this is the main process.\n");
sleep(1);
    }*/

    pthread_join(id,NULL);
    printf("this is the main process.\n");
    //flag=0;
    return(0);
}


含多参多线程,将多参封装到结构体,传递进来
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值