pthread_exit函數

void pthread_exit(void *retval); 參數:retval表示線程退出狀態,通常傳NULL。

作用:將單個線程退出。

注意幾點:

  1. return的作用是返回到函數的調用點,如果是main函數中的return,則代表該進程結束,并釋放進程地址空間,所有線程都終止。對于其它函數的return,則直接返回到函數的調用點。exit和_exit函數會直接終止整個進程,導致所有線程結束。pthread_exit函數則會導致調用該函數的線程結束。所以,多線程環境中,應盡量少用,或者不使用exit函數,取而代之使用pthread_exit函數,將單個線程退出。任何線程里exit導致進程退出,其他線程也結束,主控線程退出時不能return或exit。
  2. 另注意,pthread_exit或者return返回的指針(線程執行的函數用return或者pthread_exit結束線程時)所指向的內存單元必須是全局的或者是用malloc分配的,不能在線程函數的棧上分配,因為當其它線程得到這個返回指針時線程函數已經退出了,此時退出的這個線程函數所占據的棧空間可能又會被重新分配出去,因此其他線程再次使用這個返回的地址沒有意義。

//代碼示例

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>void *tfn(void *arg)
{int s = (int)arg;printf("The %dth thread: the process id is %d and thread id is %lu.\n",s+1,getpid( ),pthread_self( ));return NULL;
}int main(int argc,char *argv[ ])
{pthread_t tid;int ret, i, n=5;if(argc == 2)n = atoi(argv[1]);for( i=0;i<n;i++ ){ret = pthread_create(&tid, NULL, tfn,(void *)i);if( ret != 0 ){fprintf(stderr,"pthread_create error: %s\n",strerror(ret));exit(1);}}printf("In main: the process id is %u and thread id is %lu.\n",getpid( ),pthread_self( ));pthread_exit( NULL );
}

[root@localhost 01_pthread_test]# ./pthrd_crt 10

The 1th thread: the process id is 9035 and thread id is 4149640000.

The 4th thread: the process id is 9035 and thread id is 4124461888.

The 7th thread: the process id is 9035 and thread id is 4099283776.

The 9th thread: the process id is 9035 and thread id is 4082498368.

In main: the process id is 9035 and thread id is 4151699712.

The 6th thread: the process id is 9035 and thread id is 4107676480.

The 8th thread: the process id is 9035 and thread id is 4090891072.

The 5th thread: the process id is 9035 and thread id is 4116069184.

The 10th thread: the process id is 9035 and thread id is 4074105664.

The 2th thread: the process id is 9035 and thread id is 4141247296.

The 3th thread: the process id is 9035 and thread id is 4132854592.

分析:通過使用pthread_exit函數可以不再用sleep函數,此時線程輸出的順序也是無序的。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385295.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385295.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385295.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++面試常見問題

背景色yellow 1 1. c如何防止一個類被其他類繼承 >- 如果是僅僅為了達到這個目的可以直接把這個類的構造函數設置成私有的&#xff0c;這樣就杜絕了其他類的繼承。也相當于毀掉了這個類&#xff08;無法再創造出自己的對象&#xff09;。 那么怎么樣既要保證這個類的完整性…

pthread_join函數

int pthread_join(pthread_t thread, void **retval); 作用&#xff1a;阻塞等待線程退出&#xff0c;獲取線程退出狀態。其作用對應進程中 waitpid() 函數。 成功&#xff1a;0&#xff1b;失敗&#xff1a;錯誤號 strerror函數 參數&#xff1a;thread&#xff1a;線程I…

【C++ Priemr | 15】派生類向基類轉換的可訪問性

1. 只有當D公有繼承B時&#xff0c;用戶代碼才能使用派生類向基類的轉換&#xff1b;如果D私有繼承B的方式是受保護的或者私有的&#xff0c;則用戶代碼不能使用該轉換。 class A {}&#xff1b; class B : public A {}void function(const A&) {}int main() {B b;functio…

pthread_detach函數

int pthread_detach(pthread_t thread); 成功&#xff1a;0&#xff1b;失敗&#xff1a;錯誤號 作用&#xff1a;從狀態上實現線程分離&#xff0c;注意不是指該線程獨自占用地址空間。 線程分離狀態&#xff1a;指定該狀態&#xff0c;線程主動與主控線程斷開關系。線程…

【C++ Primer | 15】面試問題

在成員函數中調用虛函數 #include <iostream> using namespace std; class CBase { public:void func1(){func2();}virtual void func2() {cout << "CBase::func2()" << endl;} }; class CDerived:public CBase { public:virtual void func2() {…

pthread_cancel、pthread_equal函數

&#xff08;1&#xff09;pthread_cancel函數 int pthread_cancel(pthread_t thread); 成功&#xff1a;0&#xff1b;失敗&#xff1a;錯誤號 作用&#xff1a;殺死(取消)線程&#xff0c;其作用對應進程中 kill() 函數。 注意&#xff1a;線程的取消并不是實時的&…

STL源碼剖析面試問題

當vector的內存用完了&#xff0c;它是如何動態擴展內存的&#xff1f;它是怎么釋放內存的&#xff1f;用clear可以釋放掉內存嗎&#xff1f;是不是線程安全的&#xff1f; vector內存用完了&#xff0c;會以當前size大小重新申請2* size的內存&#xff0c;然后把原來的元素復制…

線程與進程的控制原語對比

線程與進程的控制原語對比 fork pthead_create exit( int ) pthead_exit(void *); wait(int *) pthread_join&#xff08; ,void **&#xff09; 阻塞 ;分離 22 &#xff1b;cancel -1 kill() pthread_cancel(); 取消點(檢查點)&#xff1a;系統調用 getpid() pthrea…

ptmalloc堆內存管理機制(主要討論Linux x86下32位系統)

bin&#xff08;chunk容器&#xff09; ptmalloc將相似大小的 chunk 用雙向鏈表鏈接起來&#xff0c;這樣的一個鏈表被稱為一個 bin。 Ptmalloc 一共維護了 128 個 bin&#xff0c;并使用一個數組來存儲這些 bin&#xff0c;這個數組被成為bin數組。 bin數組結構如下&#xf…

線程屬性的修改

&#xff08;1&#xff09;線程屬性 Linux下線程的屬性是可以根據實際項目需要&#xff0c;進行設置&#xff0c;之前我們討論的線程都是采用線程的默認屬性&#xff0c;默認屬性已經可以解決絕大多數開發時遇到的問題。如我們對程序的性能提出更高的要求那么需要設置線程屬性…

NPTL(Native POSIX Thread Library)

1.NPTL&#xff08;Native POSIX Thread Library&#xff09;為POSIX標準線程庫&#xff0c;查看當前Linux系統的pthread庫&#xff08;線程庫&#xff09;版本的命令為&#xff1a;getconf GNU_LIBPTHREAD_VERSION。 [rootlocalhost 01_pthread_test]# getconf GNU_LIBPTHREA…

線程使用注意事項

1.主線程退出其他線程不退出&#xff0c;主線程應調用pthread_exit&#xff1b; 2.避免僵尸線程&#xff1a;pthread_join、pthread_detach、pthread_create指定分離屬性。被join線程可能在join函數返回前就釋放完自己的所有內存資源&#xff0c;所以不應當返回被回收線程棧中…

線程同步的概念

所謂同步&#xff0c;即同時起步&#xff0c;協調一致。不同的對象&#xff0c;對“同步”的理解方式略有不同。如&#xff0c;設備同步&#xff0c;是指在兩個設備之間規定一個共同的時間參考&#xff1b;數據庫同步&#xff0c;是指讓兩個或多個數據庫內容保持一致&#xff0…

互斥量(mutex)

Linux中提供一把互斥鎖mutex&#xff08;也稱之為互斥量&#xff09;。每個線程在對資源操作前都嘗試先加鎖&#xff0c;成功加鎖才能操作&#xff0c;操作結束解鎖。資源還是共享的&#xff0c;線程間也還是競爭的&#xff0c;但通過“鎖”就將資源的訪問變成互斥操作&#xf…

洗牌算法

參考資料&#xff1a; 1. 洗牌算法匯總以及測試洗牌程序的正確性 2. 三種洗牌算法shuffle

Bloom Filter算法

一、概念 Bloom Filter的中文翻譯叫做布隆過濾器&#xff0c;是1970年由布隆提出的。它實際上是一個很長的二進制向量和一系列隨機映射函數。布隆過濾器可以用于檢索一個元素是否在一個集合中。它的優點是空間效率和查詢時間都遠遠超過一般的算法&#xff0c;缺點是有一定的誤…

237. 刪除鏈表中的節點

請編寫一個函數&#xff0c;使其可以刪除某個鏈表中給定的&#xff08;非末尾&#xff09;節點&#xff0c;你將只被給定要求被刪除的節點。 現有一個鏈表 -- head [4,5,1,9]&#xff0c;它可以表示為: 示例 1: 輸入: head [4,5,1,9], node 5 輸出: [4,1,9] 解釋: 給定你鏈表…

151. 翻轉字符串里的單詞

輸入: " hello world! " 輸出: "world! hello" 解釋: 輸入字符串可以在前面或者后面包含多余的空格&#xff0c;但是反轉后的字符不能包括。 示例 3&#xff1a; 輸入: "a good example" 輸出: "example good a" 解釋: 如果兩個單…

進程間同步(互斥量、信號量)

進程間同步可以使用互斥量mutex&#xff08;互斥鎖&#xff09;、信號量和文件鎖。 進程間同步使用信號量&#xff1a; int sem_init(sem_t *sem, int pshared, unsigned int value); 用于進程間同步此時第二個參數不能取0了&#xff0c;取非0值用于進程間同步&#xff0c;一…

1059 Prime Factors(25 分)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N p?1???k?1????p?2???k?2?????p?m???k?m????. Input Specification: Each input file contains one test case which gives a…