arg是什么函数_怎么实现边听歌边搜图?线程初体验:常用函数
生活随笔
收集整理的這篇文章主要介紹了
arg是什么函数_怎么实现边听歌边搜图?线程初体验:常用函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
圖控大叔
構圖傳遞思想
閱讀從未如此簡單!!!
01
前言
? ? ? ?今天我們來認識一下線程及其常用函數。當你今天看到我在寫推文時,你可能會說:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???02
目錄總覽
2-1 什么是線程2-2?常用函數速覽2-3?各函數參數說明
2-4?例程分享
2-1
什么是線程
1,稱之為調度的最小單位,CPU的任務調度都是以線程為單位去調度的2,所有的進程里面都有至少一條線程
3,如果是同個進程的線程,共享所有資源
4,任務隊列的依賴原型
5,每個線程獨立擁有一片棧空間,默認大小為2-8M也就是說,
1、線程創建后會很認真的去一件事
2、多個線程同時運行其實是切割系統時間片
?宏觀并行,微觀串行
3、這就是標題提到的:邊聽歌邊搜圖的實現基礎
2-2
常用函數速覽
int?pthread_create(pthread_t *thread, const?pthread_attr_t *attr,void?*(*start_routine) (void?*), void?*arg);????線程創建int?pthread_cancel(pthread_t thread);
????發指令請求結束指定線程
????(包含取消點函數知識點)void?pthread_exit(void?*retval);
????線程結束自身int?pthread_join(pthread_t thread, void?**retval);
????指定線程ID,回收線程資源pthread_t pthread_self(void);
????線程獲取自身線程IDvoid?pthread_cleanup_push(void?(*routine)(void?*),void?*arg);
??????注冊函數功能:線程收到取消指令后,
??????會先執行已經注冊的函數,再退出線程void?pthread_cleanup_pop(int?execute);
??????清除注冊過的函數,線程收到取消指令后,直接退出
2-3
各函數參數說明
pthread_create函數功能:??????在進程當中創建出一條線程
????參數:
????? thread:如果進程創建成功,將會往這個內存里面填充一個線程的ID
????? attr:線程的屬性變量,創建線程的時候會依賴于這個線程的屬性變量去創建出一個線程,這個參數如果需要使用,需要調用pthread_attr_init函數初始化它。如果這個參數為NULL,則代表按照默認屬性創建。
????????線程的屬性重點包含著:1,線程的分離屬性2,線程的棧空間大小3,線程的優先級
start_routine:一個函數指針類型,新的線程將會去執行這個函數的內容
arg:這個是傳輸給start_routine這個函數的參數
????返回值:
??????成功則返回0,失敗則返回一個錯誤值,errno沒有被設置
示例:pthread_t?tid;int?retval;
retval = pthread_create( &tid, NULL, new_thread, "hello thread");
new_thread:函數名pthread_cancel函數功能:
????????給指定的線程發送一個取消指令,該線程默認收到此取消指令后回退出線程(線程運行到取消點函數的時候,哪些是取消點函數可以參照man 7 pthreads)
??????參數:
????????? thread:指定的線程ID
??????返回值:
????????成功返回0,失敗返回一個錯誤的數字,errno不會被設置pthread_exit函數功能:
????????結束自身,即退出一個線程,如果所有的線程都退出了,我們就會結束進程
????參數:
??????? retval:會將線程退出時可以返回給等待這個線程退出的函數(pthread_join)返回值:pthread_join函數功能:
????????等待指定的線程退出,并且接收其返回值。
????參數:
??????? thread:指定等待退出的線程的ID
??????? retval:用來接收線程退出的返回值的void?*的變量的地址,如果不想接收可以直接填入NULL
????返回值:
????????成功返回0,失敗返回一個錯誤的值,errno不會被設置pthread_self函數功能:獲取本線程ID:
無參數
返回值:成功返回該線程的ID值pthread_cleanup_push函數功能:
取消指令退出線程前的注冊函數,用于執行該函數過后,
如果接收到線程的取消指令,則先去執行該函數所注冊的函數后,才能退出線程
參數:
????? routine:如果接收到取消指令,則去執行這個函數指針所代表的函數的內容
????? arg:這個是傳輸給routine這個函數指針的函數的參數
示例:void?routine(void?*arg){printf("嘻嘻 arg=%s\n", (char?*)arg);
}
{//線程中
??pthread_cleanup_push(routine, "哈哈");
}pthread_cleanup_pop函數功能:
必須與上面的函數配套使用,用于清除出注冊的函數,代表以后接收到取消指令我們也不會去執行注冊的函數
參數:execute:
如果該值為0,則清除出注冊函數,而不去執行注冊函數的內容,
如果該值為非0,則清除出注冊函數的同時,去運行注冊函數的內容
2-4
例程分享
線程結束自身#include?#include?
#include?
#include?
#include?
#include?
void?*new_thread(void?*arg){
??char?*string?= arg;
??int?i;
??for(i=0; i<3; i++)
??{
????printf("arg=%s\n", string);
????sleep(1);
??}
??pthread_exit("exit");//退出一個線程
??
??//return "exit";
}
?
int?main(void){
??int?retval;
??void?*retpoint;
??pthread_t?tid;
?
??//創建一條線程
??retval = pthread_create( &tid, NULL, new_thread, "hello thread");
??if(retval != 0)
??{
????//perror("創建線程失敗:");
????fprintf(stderr, "創建線程失敗:%s\n", strerror(retval));
????return?-1;
??}
?
??pthread_join(tid, &retpoint);//等待回收指定的線程, 若tid指定的線程不退出,則主線程則阻塞在此
??
??printf("接合成功 返回值為%s\n", (char?*)retpoint);
??
??return?0;
}線程退出前的執行函數實驗#include?#include?#include?#include?#include?#include?pthread_t?tid;void?routine(void?*arg){printf("嘻嘻 arg=%s\n", (char?*)arg);
}void?*thread(void?*arg){int?i, j, z=5;//設置線程一旦有人對線程發送取消請求則馬上退出線程,不用遇到取消點函數
??pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);??//在線程被取消的時候,要求在退出線程之前執行routine里面的內容,//"哈哈"則是傳輸給routine的參數
??pthread_cleanup_push(routine, "哈哈");while(z--)
??{for( i=80000; i>0; i--)for( j=8000; j>0; j--);printf("in thread\n");
??}??//將pthread_cleanup_push所聲明如果取消線程時執行的routine函數清除出去,//0代表清除而不執行,//非0代表清除且執行一下pthread_cleanup_push所注冊的routine函數里面的內容??pthread_cleanup_pop(0);
??z=5;while(z--)
??{for( i=80000; i>0; i--)for( j=8000; j>0; j--);printf("next thread\n");
??}return???NULL;
}void?sighand(int?signum){??//給指定線程發送取消請求,線程遇到取消點函數才會退出
??pthread_cancel(tid);
}int?main(void){int?retval;??/*
??信號注冊
? SIGINT:Ctrl + c
??sighand: 按下Ctrl + c 時會去執行的函數
??*/
??signal(SIGINT, sighand);
??pthread_create(&tid, NULL, thread, NULL);??//等待線程結束,回收資源
??retval = pthread_join(tid, NULL);if(retval != 0)
??{fprintf(stderr, "join failed :%s\n", strerror(retval));return?-1;
??}printf("join success\n");return?0;
}
03
結尾
? ? ? ?內容分享,就先到這里了!
總結
以上是生活随笔為你收集整理的arg是什么函数_怎么实现边听歌边搜图?线程初体验:常用函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python open 相对路径_第十四
- 下一篇: switch语句可以被代替吗_大空间建筑