執行緒池應用例項

2021-10-09 08:47:41 字數 3037 閱讀 7727

利用執行緒池拷貝大檔案(包含資料夾和檔案)

opendir()

mkdir()

readdir()

獲取檔案屬性--

--stat

(struct stat --

--s_isreg

() 判斷普通檔案 s_isdir

() 判斷目錄檔案

#include

#include

#include

#include

#include

//遞迴讀取目錄

void

func

(char

*filepath)

//讀取目錄

struct dirent *fd_info =

null

;while

((fd_info =

readdir

(dir))!=

null)}

}}intmain

(int argc,

char

const

*ar**)

//自定義乙個函式型別

typedef

void

*(route)

(char*,

char*)

;//單向不迴圈任務鍊錶

struct task_list

;

void

*func

(char

*src,

char

*dest)

char buf[

1024];

//迴圈讀取檔案並寫入檔案

while(1

)else

break;}

fclose

(input)

;fclose

(output)

;return

null

;}

//遞迴讀取目錄

void

readdir

(char

*filepath,

char

*copypath,

struct pthread_pool *pool)

//讀取目錄

struct dirent *fd_info =

null

;while

((fd_info =

readdir

(dir))!=

null

)else}}

}

//向任務鍊錶中新增任務

void

task_add

(struct pthread_pool *pool, route *fuction,

char

*dest,

char

*src)

struct task_list *new =

malloc

(sizeof

(struct task_list));

if(new ==

null

) new->fuc = fuction;

strcpy

(new->dest, dest)

;strcpy

(new->src, src)

; new->next =

null

;//把新節點加入到鍊錶末尾

struct task_list *p = pool->list;

while

(p->next !=

null

) p->next = new;

pool->wait_task++

;//喚醒條件變數阻塞的執行緒

pthread_cond_signal

(&pool->v)

;}

//強轉執行緒結構體

struct tem

;//強轉執行緒

void

*time_delay

(void

*arg)

int

main

(int argc,

char

const

*ar**)

mkdir

(ar**[2]

,0777);

//執行緒池初始化

struct pthread_pool pool;

pthread_init

(&pool)

;//強轉執行緒

pthread_t tid;

struct tem *arg =

malloc

(sizeof

(struct tem));

strcpy

(arg->dir1, ar**[1]

);strcpy

(arg->dir2, ar**[2]

);arg->pool =

&pool;

pthread_create

(&tid,

null

, time_delay,

(void

*)arg)

;//向執行緒池新增5個執行緒

pthread_pool_add

(&pool,5)

;//等待新增任務執行緒結束

pthread_join

(tid,

null);

printf

("讀取目錄結束\n");

free

(arg)

;//自動退出執行緒池

參考鏈結

執行緒 執行緒池 執行緒同步 例項

執行緒 using system using system.collections.generic using system.text using system.threading namespace 執行緒thread public void threadmain data static void...

ThreadLocal執行緒池例項

學習了多執行緒的理論後,下面我們來看工作中遇到的乙個具體例項。public integer startareaindex listactivityids arealist if collectionutils.isempty arealist logtypeenum.activity solr.in...

Java執行緒池應用

1.背景 諸如web 伺服器 資料庫伺服器 檔案伺服器或郵件伺服器之類的許多伺服器應用程式都面向處理來自某些遠端 的大量短小的任務。伺服器應用程式中經常出現的情況是 單個任務處理的時間很短而請求的數目卻是巨大的。2.解決方法 構建伺服器應用程式的乙個過於簡單的模型應該是 每當乙個請求到達就建立乙個新...