Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
執行緒
|
249
ESRCH
thread
指定了無效的執行緒。
下面是使用範例:
int ret;
/* 連接 `thread' 而且我們並不在乎它的傳回值 */
ret = pthread_join (thread, NULL);
if (ret) {
errno = ret;
perror ("pthread_join");
return -1;
}
分離執行緒
預設情況下,執行緒都是
可連接的
joinable
)。然而,你可以
分離
detach
)執行緒,
使其不再可連接。因為執行緒會耗用系統資源直到被連接為止,正如行程會耗用系統資
源直到其父行程叫用
wait()
,你不打算連接的執行緒應該予以分離。
#include <pthread.h>
int pthread_detach (pthread_t thread);
調用成功時,
pthread_detach()
會分離
thread
所指定的執行緒,並傳回零值。如果被你
調用
pthread_detach()
的執行緒早已被分離,則結果未定義。發生錯誤時,此函式會傳
ESRCH
,代表
thread
是無效的。
你應該對行程中的每個執行緒調用
pthread_join()
pthread_detach()
,以便在執行緒
終止時釋出系統資源。(當然,如果整個行程都結束了,那麼所有執行緒的資源都會被
釋出,不過明確連接或分離所有執行緒仍是好的做法。)
執行緒範例程式
接著讓我們來看一支完整的範例程式,其中用到了目前為止我們討論過的介面。它會
建立 2 個執行緒(總共有 3 個),在同一個起始常式 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AGILE學習手冊 | SCRUM、XP、精實和看板方法

AGILE學習手冊 | SCRUM、XP、精實和看板方法

Andrew Stellman, Jennifer Greene
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812