栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux C/C++线程

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux C/C++线程

文章目录
  • 一、线程是什么?
  • 二、线程创建
  • 三、线程终止
  • 四、线程分离
  • 五、线程连接
  • 六、线程属性
  • 总结


一、线程是什么?

(英语:thread)是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务。在Unix System V及SunOS中也被称为轻量进程(lightweight processes),但轻量进程更多指内核线程(kernel thread),而把用户线程(user thread)称为线程。

二、线程创建

线程创建函数: int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

#include
#include
#include
#include

void* callback(void* arg)
{
    printf("child thread...n");
    printf("arg value:%dn",*(int*)arg);
    return NULL;
}

int main()
{
    pthread_t tid;
    int num=10;
    //创建一个子线程
    int ret = pthread_create(&tid,NULL,callback,(void *)&num);

    if(ret!=0)
    {
        char* errstr = strerror(ret);
        printf("error:%sn",errstr);
    }

    for(int i=0;i<5;i++)
    {
        printf("%dn",i);
    }

    sleep(1);
    return 0;
}
三、线程终止

线程终止函数: void pthread_exit(void *retval);

#include
#include
#include
#include

void* callback(void* arg)
{
    printf("child thread id:%ldn",pthread_self());
    return NULL;//pthread_exit(NULL);
}

int main()
{
    //创建一个子进程
    pthread_t tid;
    int ret = pthread_create(&tid,NULL,callback,NULL);

    if(ret!=0)
    {
        char* errstr= strerror(ret);
        printf("error:%sn",errstr);
    }

    //主线程
    for(int i=0;i<5;i++)
    {
        printf("%dn",i);
    }

    printf("tid:%ld,man thread id:%ldn",tid,pthread_self());

    //让主线程退出,当主线程退出时,不会影响其他正常运行的线程。
    pthread_exit(NULL);

    printf("main thread");

    return 0;
}
四、线程分离

线程分离函数: int pthread_detach(pthread_t thread);

#include
#include
#include
#include
#include

void* callback(void* arg)
{
   printf("child thread id: %ldn",pthread_self()); 

}

int main()
{
    //创建一个子线程
    pthread_t tid;

    int ret = pthread_create(&tid,NULL,callback,NULL);
    if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error1: %sn",errstr);
    }

    //输出主线程和子线程的id
    printf("tid: %ld,main thread id: %ldn",tid,pthread_self());

    //设置子线程分离,子线程分离后,子线程结束时对应的资源就不需要主线程释放
    ret = pthread_detach(tid);
    if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error2: %sn",errstr);
    }

    //设置分离后,对分离的子线程进行连接
    ret = pthread_join(tid,NULL);
     if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error3: %sn",errstr);
    }

    pthread_exit(NULL);


    return 0;
}
五、线程连接

线程连接函数: int pthread_join(pthread_t thread, void **retval);

#include
#include
#include
#include

int value=10;

void* callback(void* arg)
{
    printf("child thread id:%ldn",pthread_self());
   // sleep(3);
   // int value=10;//局部变量
    pthread_exit((void*)&value); //return (void*)&value;
}

int main()
{
    //创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid,NULL,callback,NULL);

    if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error:%sn",errstr);
    }

    //主线程
    for(int i=0;i<5;i++)
    {
        printf("%dn",i);
    }

    printf("tid :%ld,main thread id:%ldn",tid,pthread_self());

    //主线程调用pthread_join()回收子线程的资源
    int* thread_retval;
    ret = pthread_join(tid,(void**)&thread_retval);
    if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error:%sn",errstr);
    }

    printf("exit data:%dn",*thread_retval);


    printf("回收子线程资源成功!n");

    pthread_exit(NULL);
    

    return 0;
}
六、线程属性

线程属性函数:

int pthread_attr_init(pthread_attr_t* attr);
-功能:初始化线程属性变量

int pthread_attr_destroy(pthread_attr_t *attr);
-功能:释放线程属性资源

int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
-功能:获取线程分离的状态属性

int pthread_attr_getdetachstate(const pthread_attr_t *attr, int
*detachstate);
-功能:设置线程分离的状态属性

#include
#include
#include
#include

void* callback(void* arg)
{
   printf("child thread id: %ldn",pthread_self()); 

}

int main()
{
    //创建一个线程属性变量
    pthread_attr_t attr;
    //初始化属性变量
    pthread_attr_init(&attr);

    //设置属性
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    //创建一个子线程
    pthread_t tid;

    int ret = pthread_create(&tid,&attr,callback,NULL);
    if(ret!=0)
    {
        char* errstr=strerror(ret);
        printf("error1: %sn",errstr);
    }

    //获取线程栈的大小
    size_t size;
    pthread_attr_getstacksize(&attr,&size);
    printf("thread stack size:%ldn",size);

    //输出主线程和子线程的id
    printf("tid: %ld,main thread id: %ldn",tid,pthread_self());


    //释放线程属性资源
    pthread_attr_destroy(&attr);

    pthread_exit(NULL);


    return 0;
}

总结

今天主要介绍了线程的基本操作及函数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836181.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号