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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、嵌入式Linux高級編程,Linux線程編程,Linux線程概述,進程是系統(tǒng)中程序執(zhí)行和資源分配的基本單位。每個進程有自己的數(shù)據(jù)段、代碼段和堆棧段。 線程通常叫做輕型的進程。線程是在共享內存空間中并發(fā)執(zhí)行的多道執(zhí)行路徑,他們共享一個進程的資源。 因為線程和進程比起來很小,所以相對來說,線程花費更少的CPU資源。,圖1 進程與線程的關系,創(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:線程標識符(唯一性) attr:線程屬性設置 start_routine:線程函數(shù)起始地址 arg:傳遞給start_routine的參數(shù) 注意:由于pthread.h不是Linux系統(tǒng)默認的庫,連接時需要使用庫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 ) 功能:線程主動退出。 參數(shù): retval:pthread

4、_exit調用者線程的返回值,可由其他函數(shù)和pthread_join來檢測獲取。 #include int pthread_cancel(pthread_t thread); 功能:取消指定線程。 返回值:成功返回0。 區(qū)別:pthread_exit結束自己,pthread_cancel結束別人,等待線程,#include int pthread_join(pthread_t th,void *thread_return) 功能:等待指定線程。 返回值:成功返回0。 參數(shù): th:等待線程的標識符 thread_return:用戶定義指針,用來存儲被等待線程的返回值。,#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) 多個線程同時訪問共享數(shù)據(jù)時可能會沖突,這跟前面講信號時所說的可重入性是同樣的問題。比如兩個線程都要把某個全局變量增加1,這個操作在某平臺需要三條指令完成: 從內存讀變量值到寄存器 寄存器的值加1 將寄存器的值寫回內存 假設兩個線程在多處理器平臺上同時執(zhí)行這三條指令,則可能導致下圖所示的結果,最后變量只加了一次而非兩次。,互斥量(mutex),mutex是一種簡單的加鎖的方法來控制對共享資源的訪問。在同一時刻只能有一個線程掌握某個互斥上的鎖,擁有上鎖狀態(tài)的線程能夠對共享資源進行訪問。若其他線程希望上鎖一個已經(jīng)被上了互斥鎖的資源,則該線程掛起,直到上鎖的線程釋放互斥鎖

7、為止。 互斥鎖的操作主要包括以下幾個步驟: 互斥鎖初始化: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); 功能:訪問資源,鎖資源 返回值:成功返回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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論