黑马《linux系统编程》学习笔记(从71到75)

七十一. pthread_exit函数的使用

 pthread_exit.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h> //这里是线程对应的头文件

void* myfunc(void* arg)
{
	//打印子进程的id
    printf("child pthread id: %lu\n", pthread_self());
    for(int i=0;i<5;++i)
	{
		printf("child i = %d\n",i);
	}
	return NULL;
}

int main(int argc, const char* argv[])
{
    // 创建子线程
    //线程ID变量
	int ret = pthread_create(thid, NULL, myfunc, NULL); 
	if(ret != 0)
	{
		printf("error number: %d\n",ret);
		//打印错误信息
		printf("%s\n",strerror(ret));
	}
    printf("parent pthread id: %lu\n", pthread_self());
	
	//退出主线程
	pthread_exit(NULL);
	
    for(int i=0;i<5;++i)
	{
		printf("parent child i = %d\n",i);
	}
    return 0;
}

运行结果,由于pthread_exit()让主线程退出,所以主程序最后的循环部分,并不会执行

[root@VM_0_15_centos 8Day]# gcc pthread_exit.c -o p_e -std=gnu99 -lpthread
[root@VM_0_15_centos 8Day]# ls
a.out                  p_e             pthread_attr.c    pthread_join.c
deamon.c               process_r.c     pthread_create.c  pthread_uncle.c
loop_pthread_create.c  process_work.c  pthread_exit.c    setsid.c
[root@VM_0_15_centos 8Day]# ./p_e
parent pthread id: 139925119584064
child pthread id: 139925111203584
child thread .....
child i = 0
child i = 1
child i = 2
child i = 3
child i = 4

注意,对于子线程的退出,应该用pthread_exit,而不该用exit(),因为exit()是让整个进程,而非仅此线程退出。

七十二. 使用pthread_join回收子线程资源

pthread_join.c 

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>

int number = 100;
void* myfunc(void* arg)
{
    printf("child pthread id: %lu\n", pthread_self());
    printf("child thread .....\n");
    for(int i=0; i<5; ++i)
    {
        printf("child i = %d\n", i);
    }
    return &number;
    //pthread_exit(&number);
}

int main(int argc, const char* argv[])
{
    // 创建子线程
    pthread_t thid;
    // 返回错误号
    int ret = pthread_create(&thid, NULL, myfunc, NULL);
    if(ret != 0)
    {
        printf("error number: %d\n", ret);
        // 根据错误号打印错误信息
        printf("error information: %s\n", strerror(ret));
    }
    printf("parent pthread id: %lu\n", pthread_self());

    // 退出主线程,子线程不受影响
//    pthread_exit(NULL);
    int *ptr;
    pthread_join(thid, (void**)&ptr);
    printf("++++++++++ number = %d\n", *ptr);

    printf("parent thread .....\n");
    for(int i=0; i<3; ++i)
    {
        printf("i = %d\n", i);
    }

    return 0;
}

运行一下

[root@VM_0_15_centos 8Day]# gcc pthread_join.c -o p_j -std=gnu99 -lpthread
[root@VM_0_15_centos 8Day]# ls
a.out                  p_e          process_work.c    pthread_exit.c   setsid.c
deamon.c               p_j          pthread_attr.c    pthread_join.c
loop_pthread_create.c  process_r.c  pthread_create.c  pthread_uncle.c
[root@VM_0_15_centos 8Day]# ./p_j
parent pthread id: 140315507676992
child pthread id: 140315499296512
child thread .....
child i = 0
child i = 1
child i = 2
child i = 3
child i = 4
++++++++++ number = 100
parent thread .....
i = 0
i = 1
i = 2

七十三. 线程相关函数介绍

七十四. 设置分离属性

pthread_attr.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>

void* myfunc(void* arg)
{
    printf("child pthread id: %lu\n", pthread_self());
    return NULL;
}

int main(int argc, const char* argv[])
{
    // 创建子线程
        //线程ID变量
    pthread_t thid;
    // 返回错误号
    //初始化线程的属性
    pthread_attr_t attr;
        pthread_attr_init(&attr);
        //设置分离
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);


    int ret = pthread_create(&thid, &attr, myfunc, NULL);
    if(ret != 0)
    {
        printf("error number: %d\n", ret);
        // 根据错误号打印错误信息
        printf("error information: %s\n", strerror(ret));
    }
    printf("parent pthread id: %lu\n", pthread_self());

    sleep(1);

        //释放资源
        pthread_attr_destroy(&attr);
    return 0;
}

七十五. 线程同步的概念

猜你喜欢

转载自blog.csdn.net/garrulousabyss/article/details/85408565