Linux併發程式設計 執行緒講解

2021-10-14 13:12:53 字數 921 閱讀 9940

2 linux執行緒庫

3 執行緒建立 - pthread_create

4 執行緒** - pthread_join

5 執行緒結束 - pthread_exit

6 執行緒例項

首先程序有如下特點:

pthread執行緒庫提供了如下基本操作

同步和互斥機制

#include

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*routine)(void *), void *arg);

#include

int pthread_join (pthread_t thread, void **retval);

#include

void pthread_exit(void *retval);

char message[32] = "hello world"; //定義全域性字串陣列,在靜態儲存區,

//可以被所有執行緒訪問

void *thread_func(void *arg); //執行緒要執行函式

int main(void)

pthread_join(&a_thread, &result);

printf("result is %s\n", result);

printf("message is %s\n", message);

return 0;

}void thread_func(void *arg)

執行指令

$ gcc -o test test.c -l pthread

$./test

結果:thank you for waiting for me

masked by thread

併發程式設計 CountDownLatch 講解

countdownlatch 介紹 countdownlatch 是用於等待執行緒的執行,等待執行緒數量為 0 後,則結束等待,執行該執行緒等待後所要執行的 例子如下 private static countdownlatch cadownlatch new countdownlatch 2 pub...

併發基礎 Linux多執行緒程式設計

linux 下的多執行緒程式設計使用pthread posix thread 函式庫,使用時包含標頭檔案pthread.h,鏈結共享庫libpthread.so。這裡順便說一下gcc鏈結共享庫的方式 l用來指定共享庫所在目錄,系統庫目錄不用指定。l用來指定要鏈結的共享庫,只需要指定庫的名字就行了,如...

併發程式設計 2 程序 併發和並行講解

概要 程序併發和並行 同步 非同步 阻塞 非阻塞 程序的建立 結束與併發的實現 一.程序講解 程序 process 是計算機中的程式關於某資料集合上的一次執行活動,是系統進行資源分配和排程的基本單位,是作業系統結構的基礎。在早期面向程序設計的計算機結構中,程序是程式的基本執行實體 在當代面向執行緒設...