Pthread並行程式設計總結

2021-08-19 18:12:48 字數 4184 閱讀 4130

2. 執行緒資料共享

3. pthread 「hello world」

4. pthread 其他基礎 api

5.綜合例:多個陣列排序

int pthread_create(pthread_t *, 

const pthread_attr_t *,

void * (*)(void *),

void *)

呼叫例:

errcode =pthread_create(&thread_id, &thread_attribute,&thread_fun, &fun_arg);
pthread_create的效果

乙個簡單的執行緒例子

int main()

for(tn=0;tn<16;tn++)

return

0;}

這段**建立了16個執行緒執行函式「parfun」.

注意:建立執行緒的代價很高,因此parfun應完成很多任務作才值得付出這種代價

char *message = "hello world!\n";     

pthread_create( &thread1,

null,

(void*)&print_fun,

(void*) message);

3.1 一些準備

3.2pthread_join函式

int pthread_join(pthread_t *, void **value_ptr);
說明:

3.3 「hello world」

#include 

#include

#include

/* global variable: accessible to all threads */

int thread_count;

// 執行緒執行函式

void* hello(void* rank); /* thread function */

int main(int argc, char* argv) /* main */

void* hello(void* rank) /* hello */

可能的輸出結果:

hello from thread 1of4

hello from thread 3of4

hello from thread 0of4

hello from

the main thread

hello from thread 2

of4

4.1pthread_exit( )
void pthread_exit(void *value_ptr);
通過value_ptr返回結果給呼叫者

4.2pthread_cancal()

int pthread_cancel(pthread_t thread);
取消執行緒thread執行

乙個取消執行緒執行的例子

#include 

#include

#include

#include

void *threadfunc(void *parm)

}int main(int argc, char *argv)

執行結果:

i am the child thread

.i am the child thread

.i am the child thread

.i am the child thread

.the child thread has been canceled.

​ 乘法並行採取資料劃分,把資料分配給不同的程序/執行緒,

#include #include #include #include #include #include #include using namespace std;

typedef

struct threadparm_t;

const

int arr_num = 10000;

const

int arr_len = 10000;

const

int thread_num = 4;

const

int seg = arr_num / thread_num;//陣列數/執行緒數=每個執行緒的任務量

vector arr[arr_num];

pthread_mutex_t mutex;

long

long head, freq; // timers

void init(void)

}void *arr_sort(void *parm)

int main(int argc, char *argv)

for (int i = 0; i < thread_num; i++)

pthread_mutex_destroy(&mutex);

}

結果:

//單執行緒

thread

0: 7581.931894ms.

//4執行緒

thread

3: 1942.302817ms.

thread

2: 1948.374916ms.

thread

0: 1955.479851ms.

thread

1: 1969.761978ms.

雖然資料完全隨機,但每個執行緒資料分布是一致的,因此達到了負載均衡。

如果生成的是不是同一分布的隨機數,結果就沒有這麼好。如下:

void init_2(void)

}

前1/4:完全公升序

第二段:1/4逆序,3/4公升序

第三段:1/2逆序,1/2公升序

第四段:完全逆序

塊劃分負載不均!

//單執行緒

thread

0: 1643.106837ms.

// 4執行緒

thread

0: 428.869616ms.

thread

1: 486.402280ms.

thread

2: 530.073299ms.

thread

3: 643.510582ms

並行代價是643.5*4!

動態任務分配

int next_arr = 0;

pthread_mutex_t mutex_task;

void *arr_sort_fine(void *parm)

pthread_mutex_lock(&mutex);

queryperformancecounter((large_integer *)&tail);

printf("thread %d: %lfms.\n", r, (tail - head) * 1000.0 / freq);

pthread_mutex_unlock(&mutex);

pthread_exit(nullptr);

}

結果:

thread

0: 549.246907ms.

thread

3: 552.934092ms.

thread

2: 556.541263ms.

thread

1: 559.427082ms

粗粒度動態劃分——每次分配50行 :

thread

0: 520.849620ms.

thread

1: 524.470671ms.

thread

3: 527.458957ms.

thread

2: 530.890995ms.

細粒度任務劃分會負載均衡,但是同步開銷也很大,至於怎樣劃分粒度合適,還需實驗。

pthread平行計算入門

實現並行的庫有很多,比如mpi庫,openmp等,同樣pthread也是實現並行的乙個庫。pthread實現並行的是共享記憶體的方式,即如何cpu都可以訪問相同的記憶體區域。這種實現方式實現起來比較簡單,但是會存在臨界區等問題。以下是最簡單的一段並行的 其實現了建立多個執行緒,為多執行緒函式傳遞引數...

C 並行開發Pthread之執行緒(一)

執行緒物件 pthread t 狀態 可執行 執行 停止 休眠 阻塞 pthread 庫可用於建立 維護和管理多執行緒和應用程式中的執行緒。1.建立執行緒 int pthread create pthread t restrict thread,const pthread attr t restri...

pthread庫使用總結

一.pthread執行緒庫 符合posix標準,portbale opeating system inte ce of unix。pthread庫分別支援linux windows版本。二.庫函式 執行緒操作函式 1.pthread create 建立乙個執行緒 2.pthread exit 終止當...