#pragma once
#ifndef thread_pool_h
#define thread_pool_h
#include
#include
#include
#include
//#include
//#include
//#include
#include
namespace std
;//執行緒池是否執行
atomic<
int> _idlthrnum
;//空閒執行緒數量
public
:inline
threadpool
(unsigned
short size =4)
inline
~threadpool()
}public
:// 提交乙個任務
// 呼叫.get()獲取返回值會等待任務執行完,獲取返回值
// 有兩種方法可以實現呼叫類成員,
// 一種是使用 bind: .commit(std::bind(&dog::sayhello, &dog));
// 一種是用 mem_fn: .commit(std::mem_fn(&dog::sayhello), this)
template
<
classf,
class..
. args>
auto
commit
(f&& f, args&&..
. args)
->future<
decltype(f
(args...
))>
;//對當前塊的語句加鎖 lock_guard 是 mutex 的 stack 封裝類,構造的時候 lock(),析構的時候 unlock()
_tasks.
emplace
([task]()
) 放到佇列後面
(*task)()
;});
}#ifdef threadpool_auto_grow
if(_idlthrnum <
1&& _pool.
size()
< threadpool_max_num)
addthread(1
);#endif
// !threadpool_auto_grow
_task_cv.
notify_one()
;// 喚醒乙個執行緒執行
return future;
}//空閒執行緒數量
intidlcount()
//執行緒數量
intthrcount()
#ifndef threadpool_auto_grow
private
:#endif
// !threadpool_auto_grow
//新增指定數量的執行緒
void
addthread
(unsigned
short size)
; _task_cv.
wait
(lock,
[this])
;// wait 直到有 taskif(
!_run && _tasks.
empty()
)return
; task =
move
(_tasks.
front()
);// 按先進先出從佇列取乙個 task
_tasks.
pop();
} _idlthrnum--
;task()
;//執行任務
_idlthrnum++;}
}); _idlthrnum++;}
}};}
#endif
//
#include
"threadpool.h"
#include
#include
void
fun1
(int slp)
}struct gfun };
class
astatic std::string bfun
(int n, std::string str,
char c)};
intmain()
try;
a a;
std::future<
void
> ff = executor.
commit
(fun1,0)
; std::future<
int> fg = executor.
commit
(gfun,0
);std::future<
int> gg = executor.
commit
(a.afun,
9999);
//ide提示錯誤,但可以編譯執行
std::future gh = executor.
commit
(a::bfun,
9998
,"mult args"
,123);
std::future fh = executor.
commit([
]()-
>std::string );
std::cout <<
" *****== sleep *****==== "
<< std::this_thread::
get_id()
<< std::endl;
std::this_thread::
sleep_for
(std::chrono::
microseconds
(900))
;for
(int i =
0; i <
50; i++
) std::cout <<
" *****== commit all *****==== "
<< std::this_thread::
get_id()
<<
" idlsize="
<< std::endl;
std::cout <<
" *****== sleep *****==== "
<< std::this_thread::
get_id()
<< std::endl;
std::this_thread::
sleep_for
(std::chrono::
seconds(3
)); ff.
get();
//呼叫.get()獲取返回值會等待執行緒執行完,獲取返回值
std::cout << fg.
get(
)<<
" "
<< fh.
get().
c_str()
<<
" "
<< std::this_thread::
get_id()
<< std::endl;
std::cout <<
" *****== sleep *****==== "
<< std::this_thread::
get_id()
<< std::endl;
std::this_thread::
sleep_for
(std::chrono::
seconds(3
)); std::cout <<
" *****== fun1,55 *****==== "
<< std::this_thread::
get_id()
<< std::endl;
executor.
commit
(fun1,55)
.get()
;//呼叫.get()獲取返回值會等待執行緒執行完
std::cout <<
"end... "
<< std::this_thread::
get_id()
<< std::endl;
std::threadpool pool(4
);std::vector< std::future<
int>
> results;
for(
int i =
0; i <8;
++i)))
;}std::cout <<
" *****== commit all2 *****==== "
<< std::this_thread::
get_id()
<< std::endl;
for(
auto
&& result : results)
std::cout << result.
get(
)<<
' ';
std::cout << std::endl;
return0;
}catch
(std::exception& e)
母函式詳解(轉 侵刪)
母函式與排列組合 在談論母函式問題之前,我們先看乙個簡單的問題描述 假如有兩組資料 a,b 和 c,d 每組中選出乙個構成乙個組合,總共有幾種選法?很顯然總共有4種選法 ac,ad,bc,bd。而且很容易聯想到這個式子 a b c d a c a d b c b d。式子中的幾個乘積項就是上面的4種...
C語言庫函式 侵刪
1.strlen 標頭檔案 include strlen 函式用來計算字串的長度,其原型為 unsigned int strlen char s s為指定的字串 include includeint main 執行結果 strlen str1 38,sizeof str1 4 strlen str1...
Linux系統常用監控 侵立刪
注 本文首發於my personal blog,歡迎光臨小站 本文內容腦圖如下 top 命令我想大家都挺熟悉吧!linux 下的 top 命令有點類似於 windows下的任務管理器,能夠實時動態地監控並顯示系統中各個程序的資源占用狀況,是 linux下常用的效能監控和分析工具。乙個常見的top命令...