嵌入式Linux高級(jí)編程--05posix_線程編程.ppt_第1頁(yè)
嵌入式Linux高級(jí)編程--05posix_線程編程.ppt_第2頁(yè)
嵌入式Linux高級(jí)編程--05posix_線程編程.ppt_第3頁(yè)
嵌入式Linux高級(jí)編程--05posix_線程編程.ppt_第4頁(yè)
嵌入式Linux高級(jí)編程--05posix_線程編程.ppt_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、嵌入式Linux高級(jí)編程,Linux線程編程,Linux線程概述,進(jìn)程是系統(tǒng)中程序執(zhí)行和資源分配的基本單位。每個(gè)進(jìn)程有自己的數(shù)據(jù)段、代碼段和堆棧段。 線程通常叫做輕型的進(jìn)程。線程是在共享內(nèi)存空間中并發(fā)執(zhí)行的多道執(zhí)行路徑,他們共享一個(gè)進(jìn)程的資源。 因?yàn)榫€程和進(jìn)程比起來(lái)很小,所以相對(duì)來(lái)說(shuō),線程花費(fèi)更少的CPU資源。,圖1 進(jìn)程與線程的關(guān)系,創(chuàng)建線程,#include int pthread_create(pthread_t *thread, pthread_attr_t *attr,void *(*start_routine)(void *),void *arg) 功能:創(chuàng)建線程。 返回值:成功返

2、回0。 參數(shù): thread:線程標(biāo)識(shí)符(唯一性) attr:線程屬性設(shè)置 start_routine:線程函數(shù)起始地址 arg:傳遞給start_routine的參數(shù) 注意:由于pthread.h不是Linux系統(tǒng)默認(rèn)的庫(kù),連接時(shí)需要使用庫(kù)libpthread.a gcc -o pthread -lpthread pthread.c,#include #include #include #include #include pthread_t ntid; void printids(const char *s) pid_t pid; pthread_t tid; pid = getpid();

3、 tid = pthread_self(); printf(%s pid %u tid %u (0 x%x)n,s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid); void *thr_fn(void *arg) printids(new thread:); return (void *)0); int main() int err; err = pthread_create( ,線程退出,#include void pthread_exit( void *retval ) 功能:線程主動(dòng)退出。 參數(shù): retval:pthread

4、_exit調(diào)用者線程的返回值,可由其他函數(shù)和pthread_join來(lái)檢測(cè)獲取。 #include int pthread_cancel(pthread_t thread); 功能:取消指定線程。 返回值:成功返回0。 區(qū)別:pthread_exit結(jié)束自己,pthread_cancel結(jié)束別人,等待線程,#include int pthread_join(pthread_t th,void *thread_return) 功能:等待指定線程。 返回值:成功返回0。 參數(shù): th:等待線程的標(biāo)識(shí)符 thread_return:用戶定義指針,用來(lái)存儲(chǔ)被等待線程的返回值。,#include #in

5、clude #include #include void *thr_fn1(void *arg) printf(thread 1 returning n); return (void*)1; void * thr_fn2(void *arg) printf(thread 2 exitingn); pthread_exit(void *)2); void *thr_fn3(void *arg) while(1) printf(thread 3 writingn); sleep(1); int main() pthread_t tid; void *tret; pthread_create( ,線

6、程同步,互斥量(mutex) 多個(gè)線程同時(shí)訪問(wèn)共享數(shù)據(jù)時(shí)可能會(huì)沖突,這跟前面講信號(hào)時(shí)所說(shuō)的可重入性是同樣的問(wèn)題。比如兩個(gè)線程都要把某個(gè)全局變量增加1,這個(gè)操作在某平臺(tái)需要三條指令完成: 從內(nèi)存讀變量值到寄存器 寄存器的值加1 將寄存器的值寫(xiě)回內(nèi)存 假設(shè)兩個(gè)線程在多處理器平臺(tái)上同時(shí)執(zhí)行這三條指令,則可能導(dǎo)致下圖所示的結(jié)果,最后變量只加了一次而非兩次。,互斥量(mutex),mutex是一種簡(jiǎn)單的加鎖的方法來(lái)控制對(duì)共享資源的訪問(wèn)。在同一時(shí)刻只能有一個(gè)線程掌握某個(gè)互斥上的鎖,擁有上鎖狀態(tài)的線程能夠?qū)蚕碣Y源進(jìn)行訪問(wèn)。若其他線程希望上鎖一個(gè)已經(jīng)被上了互斥鎖的資源,則該線程掛起,直到上鎖的線程釋放互斥鎖

7、為止。 互斥鎖的操作主要包括以下幾個(gè)步驟: 互斥鎖初始化:pthread_mutex_init 互斥鎖上鎖:pthread_mutex_lock 互斥鎖解鎖:pthread_mutex_unlock 消除互斥鎖:pthread_mutex_destroy,互斥量(mutex),#include int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); 功能:初始化鎖。 返回值:成功返回0。 #include int pthread_mutex_lock(pthread_mutex_

8、t *mutex); 功能:訪問(wèn)資源,鎖資源 返回值:成功返回0。,#include int pthread_mutex_unlock(pthread_mutex_t *mutex); 功能:解鎖 返回值:成功返回0,互斥量(mutex),互斥量(mutex),#include int pthread_mutex_destroy(pthread_mutex_t *mutex); 功能:銷毀鎖 返回值:成功返回0,#include #include #include #define NLOOP 5000 int counter; void *doit(void *); int main(int argc, char *argv) pthread_t tidA, tidB; pthread_create( ,#include #include #include #define NLOOP 5000 int counter; pthread_mutex_t

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論