https://www.boost.org/doc/libs/1_79_0/libs/context/doc/html/context/performance.html
https://www.boost.org/doc/libs/1_79_0/libs/fiber/doc/html/fiber/overview/implementations__fcontext_t__ucontext_t_and_winfiber.html

完成了boost.asio boost.beast boost.fiber基于work_stealing调度模型的整合

sched_yield()和nanosleep()对进程调度的影响
https://www.cnblogs.com/arnoldlu/p/11287972.html

std::this_thread::yield()使用理解
https://blog.csdn.net/liuhhaiffeng/article/details/52604052

1
2
3
4
5
//    while(1) std::this_thread::yield();
// t1.detach();
size_t cpu_number_out = 0;
pthread_cpu_number_np(&cpu_number_out);
std::cout << cpu_number_out << std::endl;

Windows event notification
https://github.com/python-trio/trio/issues/52#issuecomment-424591743
https://stackoverflow.com/questions/41527964/is-there-any-way-to-use-iocp-to-notify-when-a-socket-is-readable-writeable

libbfd
https://blog.csdn.net/crazycoder8848/article/details/51456297

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Windows does not provide sleep functionality with nanosecond resolution, so we
// try to approximate this with spinning combined with yielding if another thread
// is ready to run on the current processor.
void os::naked_short_nanosleep(jlong ns) {
assert(ns > -1 && ns < NANOUNITS, "Un-interruptable sleep, short time use only");

int64_t start = os::javaTimeNanos();
do {
if (SwitchToThread() == 0) {
// Nothing else is ready to run on this cpu, spin a little
SpinPause();
}
} while (os::javaTimeNanos() - start < ns);
}

yield/sched_yield/SwitchToThread
cv条件变量

macOS/xnu
pthread_cond_wait –> sched_yield –> swtch_pri(0)
std::condition_variable –> pthread_cond_wait

API swtch_pri/swtch/thread_switch

1
2
3
4
std::mutex m;
std::unique_lock<std::mutex> g(m);
std::condition_variable cv;
while(1) cv.wait(g, [](){return false;});
1
2
3
4
5
pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&mtx);
while(1) pthread_cond_wait(&cond,&mtx);
pthread_mutex_unlock(&mtx);

suspend or parking
[ref:https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/thread_parker/mod.rs]
[https://github.com/maracl/dpca_s/blob/master/common/fast_sync_utils.cpp]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Win32
// while (1) WaitForSingleObject(GetCurrentThread(), INFINITE);
#pragma comment(lib, "Synchronization.lib")
while (1) WaitOnAddress(argv, argv, sizeof(PVOID), INFINITE); // wait when not same
// NtWaitForKeyedEvent ?

Linux
pthread_cond_wait -> futex_wait -> schedule
// syscall(SYS_futex, address, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, value, NULL);
while(1) syscall(SYS_futex, argv, 0, *argv, 0); // wait when same

Mac
#include <kern/waitq.h>
thread_suspend -> thread_wait -> assert_wait(-> waitq_assert_wait64 --> waitq_assert_wait64_locked) / thread_block
semaphore_wait -> semaphore_wait_internal waitq_assert_wait64_locked
pthread_cond_wait -> __psynch_cvwait -> ksyn_wait -> thread_select/thread_block
select -> select_internal -> waitq_assert_wait64_leeway -> waitq_assert_wait64_locked

thread_act_t* threads = 0;
mach_msg_type_number_t thread_count = 0;
task_threads(current_task(), &threads, &thread_count);
while(1) thread_suspend(threads[0]);

Qt
mutex实现
UNIX sem(sysv) > cond(pthread)
MAC semaphore
WIN event

cond实习
UNIX/MAC cond(pthread)
WIN event

semaphores
linux -> futex
win -> WaitOnAddress
mac可以用_dispatch_wait_on_address 未实现

wait_on_address

Linux的libpthread属于glibc

https://blog.csdn.net/puncha/article/details/8487933
并发学习之:Keyed Events(没看懂,要调试下才能明白,伤心!)
http://undocumented.ntinternals.net/

https://cfsamson.github.io/book-exploring-async-basics

https://repositum.tuwien.at/retrieve/8679


qmutex wait
system v sem -> semop
Linux
#ifdef QT_UNIX_SEMAPHORE
sem_wait/sem_timedwait
#else
pthread_cond_wait/pthread_cond_timedwait
#endif
macOS
semaphore_wait/semaphore_timedwait

条件变量与信号量
http://events.jianshu.io/p/5a2b66ccb9a7

条件变量 -> 二元信号量
信号量 -> WaitGroup(Go)/Barrier(libuv)/pthread_barrier_wait
pthread_barrier_wait(Linux PTHREAD_BARRIER_SERIAL_THREAD)
Java(CountDownLatch)
C++ 20 std::latch/std::barrier
https://zh.cppreference.com/w/cpp/thread/latch
https://zh.cppreference.com/w/cpp/thread/barrier
多线程——CountDownLatch详解
https://www.jianshu.com/p/b90309b78775
java 与 golang 并发控制 CountDownLatch CyclicBarrier Semaphore 以及 WaitGroup 的简单使用
https://my.oschina.net/wang520/blog/4480379
https://www.apiref.com/java11-zh/java.base/java/util/concurrent/package-summary.html
Semaphore/CountDownLatch/CyclicBarrier/Phaser/Exchanger
https://www.apiref.com/java11-zh/java.base/java/util/concurrent/locks/package-summary.html
https://www.apiref.com/java11-zh/java.base/java/util/concurrent/atomic/package-summary.html
C# Mutex/Semaphore/EventWaitHandle/CountdownEvent/Barrier
https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.waithandle
https://learn.microsoft.com/zh-cn/dotnet/standard/threading/overview-of-synchronization-primitives
https://github.com/dotnet/runtime/blob/main/src/coreclr/pal/src/thread/threadsusp.cpp
https://github.com/dotnet/runtime/tree/main/src/coreclr/pal/src/synchobj
Event和Mutex区别
https://blog.csdn.net/anjen/article/details/4727791

libuv漫谈之线程
https://zhuanlan.zhihu.com/p/25973650

https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/locks/mod.rs
https://github.com/rust-lang/rust/blob/master/library/std/src/sys/unix/futex.rs
https://github.com/rust-lang/rust/tree/master/library/std/src/sys/unix/locks
pthread condvar/mutex/rwlock
https://github.com/rust-lang/rust/blob/master/library/std/src/sys/windows/locks
mutex -> AcquireSRWLockExclusive
rwlock -> AcquireSRWLockShared
conavar -> SleepConditionVariableSRW

https://github.com/rust-lang/rust/blob/master/library/std/src/sync/barrier.rs

1
2
3
4
5
pub struct Barrier {
lock: Mutex<BarrierState>,
cvar: Condvar,
num_threads: usize,
}

http://docs.libuv.org/en/v1.x/guide.html
https://pursuitking.com/go_2/
https://qinyuanpei.github.io/posts/345410188/

std::future/std::promise -> std::condition_variable
std::promise -> set_value(cv.notify_all)
std::future -> wait(cv.wait)/get(__sub_wait->cv.wait)
std::packaged_task -> ::operator()(set_value->cv.notify_all)
https://zh.cppreference.com/w/cpp/thread/promise
https://zh.cppreference.com/w/cpp/thread/future
C ++ 20中的新线程(jthread)功能
https://www.jianshu.com/p/c610ad5db6b7
When a program terminates (ie, main returns) the remaining detached threads executing in the background are not waited upon; instead their execution is suspended and their thread-local objects destructed.
https://www.itcodar.com/c-plus-1/what-is-different-between-join-and-detach-for-multi-threading-in-c.html
https://stackoverflow.com/questions/22803600/when-should-i-use-stdthreaddetach
thread — detach()的使用
https://blog.csdn.net/shunlu5586/article/details/127799458
使用join需要等它启动cv.wait_for
使用detach需要等它结束cv.wait_for?
响应式编程 And/When/Then
Promise -> When/Then
https://github.com/ReactiveX/RxCpp/blob/main/Rx/v2/src/rxcpp/rx-observable.hpp

1
2
// blocking_subscribe ->
std::condition_variable wake;

https://github.com/ReactiveX/RxJava/blob/3.x/src/main/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableLatest.java
https://github.com/ReactiveX/RxJava/blob/3.x/src/main/java/io/reactivex/rxjava3/internal/operators/flowable/BlockingFlowableLatest.java

1
final Semaphore notify = new Semaphore(0);

https://github.com/David-Haim/concurrencpp/#when_all-function
https://github.com/Naios/continuable

1
2
3
4
5
// Connect continuables through when_all, when_any or when_seq
// There are helper functions for connecting continuables:
auto all = cti::when_all(http_request("github.com"), http_request("example.com"));
auto any = cti::when_any(http_request("github.com"), http_request("example.com"));
auto seq = cti::when_seq(http_request("github.com"), http_request("example.com"));

https://github.com/ponylang/ponyc/blob/main/src/libponyrt/platform/threads.c

https://www.cnblogs.com/wuyongqiang/p/7458184.html

setjmp 的正确使用
https://blog.codingnow.com/2010/05/setjmp.html

  • setjmp/longjmp setcontext/getcontext
  • ucontext swapcontext/makecontext
  • winfiber GetThreadContext/SetThreadContext CreateFiber/SwitchToFiber
  • fcontext BOOST_JMP make_fcontext/jump_context/swap_fcontext asm

ompi
mpich

JCTools -> java

work-stealing in qt

QFuture: mention the work-stealing algorithm in the docs
https://github.com/qt/qtbase/commit/0235de994be7e04aca3456f1260b18313dd45b74

1
2
3
QThreadPool::stealAndRunRunnable
// just use simple locker -> steal idea
QMutexLocker locker(&d->mutex);