c 多執行緒 高階

2021-10-02 08:30:59 字數 1328 閱讀 8714

含義:非同步執行

引數:引數=

std::launch::async& \text\\ std::launch::deferred& \text \end

引數=)

;auto asynceager = std:

:async

(std:

:launch:

:async,

);std:

:this_thread:

:sleep_for

(std:

:chrono:

:seconds(1

));auto lazystart = asynclazy.

get(

)- begin;

auto eagerstart = asynceager.

get(

)- begin;

auto lazyduration = std:

:chrono:

:duration<

double

>

(lazystart)

.count()

;auto eagerduration = std:

:chrono:

:duration<

double

>

(eagerstart)

.count()

; std:

:cout <<

"asynclazy evaluated after : "

<< lazyduration <<

" seconds."

<< std:

:endl;

std:

:cout <<

"asynceager evaluated after: "

<< eagerduration <<

" seconds."

<< std:

:endl;

}含義:共享鎖

用法:允許多個執行緒以共享方式持有讀鎖,只允許乙個執行緒持有寫鎖

如果不使用c++17可以使用boost

例子

#include

#include

// for std::unique_lock

#include

#include

std:

:shared_mutex g_mutex;

int count =0;

void

readloop()

}void

writeloop()

}int

main()

python高階 多執行緒

1.1 獲取執行緒數 import threading import time def fun time.sleep 1 print hello t threading.thread target fun t.start print threading.enumerate while true le...

Python高階(多執行緒)

多執行緒結構 import threading def worker 子執行緒要執行的具體邏輯 函式 print threading t1 threading.current thread time.sleep 9 通過休眠模擬子執行緒非同步邏輯 print t1.getname new t thr...

C 高階特性(三)多執行緒

1,鎖 控制多執行緒併發操作時,執行緒安全問題。舉例 static bool done 執行緒是否完成標識 static object locker new object 執行緒鎖 static void main string args static void test 結果 done.threa...