SYNCHRONIZING CONCURRENT
THREADS
C++ Concurrency in Action:Chapter 4
contents
 condition variable
 async
 promise
 future
 packaged_task
 chrono
 message passing
condition_variable
Thread 1 Thread 2
cond_var mutex
cond_var.wait(
mutex,
pred);
lock
cond_var.notify_one()
wake up!
check pred
lock
release lock
wait
signal
condition_variable
 _any?
 unique_lock<mutex> or typename _Mutex
 impl?
 _Cnd_signalX, _Cnd_broadcastX, _Cnd_waitX
(xthread)
 others?
 notify_all_at_thread_exit
 _Cnd_register_at_thread_exit
 unlock and notify_all at thread exit
 spurious wakeup while (!_Pred()) wait(_Lck);
async
 async([launch, ] _Fty, args…)
 async → _Async
 _Async → _Should_launch_async: policy has async?
 _Async_state
 _Deferred_async_state
 → create _Promise
 → return future
 _Get_state_for_future
function overloadding
policy binding
launch {
async = 0x1,
deferred = 0x2,
any = async | deferred,
sync = deferred
};
async – state
_Async_State
_Deferred_async_State
_Packaged_state
_Call_immediate in Task
_Get_Value (wait for end-of-task)
_Call_immediate
_Associated_state _Run_deferred_function
_Get_Value (wait for running)
value holder with lock & notify
VARIADICTEMPLATE layer
async
 _State_manager?
 shared _State Holder (& manager)
 Task?
 ::Concurrency::task<void>([&]() {
_Call_immediate(); … }
 _Set_value(_Fn(ARGS…), false);
 others?
 _Call_deferred
 make_ready_at_thread_exit
promise
 traits
 not copyable, assignable but movable
 2 layer → _Promise – promise
 _Promise for generic, promise for template specialization
 _Promise?
 get state or future using _State_manager
 broken_promise?
 promise is valid && it not ready
 but call ~promise
future
 traits
 non copyable, assignable but movable
 asynchronous return object that holds a value :?
 get
 is valid? by _Get_only_once
 get, get, … and get
 block until ready (result or exception)
 shared_future
 copyable, assignable, movable
 but get only once!
: public _State_manager
promise – future
future
promise
get_future
get
_Promise
_State
_Get_Only_Once
_State_manager
valid – already set?
set_value
set_value_at_thread_exit
set_exception
shared_future
get
member variable
inherits
_Assoc_State sharing!
packaged_task
 traits
 asynchronous provider
 return the result of a call to a function object
 members
 _Promise
 get_future
 return future (_Get_state_for_future)
 operator()
 _Call_immediate
 why?
 separate run thread, result gather thread
promise – packaged_task
packaged_task
_Stateoperator () _Call_immediate
get_future future get
functor
_Promise
spawn_task
template<typename F, typename A>
future<result_of<F(A&&)>::type>
spawn_task(F&& f, A&& a) {
typedef result_of<F(A&&)>::type result_type;
packaged_task<result_type(A&&)> task(move(f));
future<result_type> res(task.get_future());
thread t(move(task), move(a));
t.detach();
return res;
}
wait!
 for
 wait_for duration
 until
 wait_until time point
 listing
 sleep, wait, try_lock, …
chrono
 brief
 duration
 time value with measurement unit
 time point
 since a specific point in time epoch
 keywords
 steady clock
 can be adjusted
message passing
 send signal inter-actor
manager programmercustomer
Request a product
Send functional list
message passing
manager
worker
customer
request
notify
working
servicing
studying
programmer
programming
(transition)
message passing
 in this book
 seeAppendix C
 author’s message-passing framework
C++ Concurrency in Action:Chapter 4
Synchronizing concurrent threads
End-of-File

Synchronizing concurrent threads