1. 程式人生 > >黑馬《linux系統程式設計》學習筆記(從71到75)

黑馬《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()讓主執行緒退出,所以主程式最後的迴圈部分,並不會執行

[[email protected]_0_15_centos 8Day]# gcc pthread_exit.c -o p_e -std=gnu99 -lpthread
[[email protected]_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
[
[email protected]
_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;
}

執行一下

[[email protected]_0_15_centos 8Day]# gcc pthread_join.c -o p_j -std=gnu99 -lpthread
[[email protected]_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
[[email protected]_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;
}

七十五. 執行緒同步的概念