0
点赞
收藏
分享

微信扫一扫

Linux C/C++线程

yeamy 2022-04-25 阅读 52

文章目录


一、线程是什么?

二、线程创建

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

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

void* callback(void* arg)
{
    printf("child thread...\n");
    printf("arg value:%d\n",*(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:%s\n",errstr);
    }

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

    sleep(1);
    return 0;
}

三、线程终止

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

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

void* callback(void* arg)
{
    printf("child thread id:%ld\n",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:%s\n",errstr);
    }

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

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

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

    printf("main thread");

    return 0;
}

四、线程分离

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

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

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

}

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

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

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

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

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

    pthread_exit(NULL);


    return 0;
}

五、线程连接

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

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

int value=10;

void* callback(void* arg)
{
    printf("child thread id:%ld\n",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:%s\n",errstr);
    }

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

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

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

    printf("exit data:%d\n",*thread_retval);


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

    pthread_exit(NULL);
    

    return 0;
}

六、线程属性

线程属性函数:

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

void* callback(void* arg)
{
   printf("child thread id: %ld\n",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: %s\n",errstr);
    }

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

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


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

    pthread_exit(NULL);


    return 0;
}

总结

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

举报

相关推荐

0 条评论