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...
condition_variable
 _any?
 unique_lock<mutex> or typename _Mutex
 impl?
 _Cnd_signalX, _Cnd_broadcastX, _Cnd_waitX
(xt...
async
 async([launch, ] _Fty, args…)
 async → _Async
 _Async → _Should_launch_async: policy has async?
 _Async_state
...
async – state
_Async_State
_Deferred_async_State
_Packaged_state
_Call_immediate in Task
_Get_Value (wait for end-of-task)...
async
 _State_manager?
 shared _State Holder (& manager)
 Task?
 ::Concurrency::task<void>([&]() {
_Call_immediate(); ...
promise
 traits
 not copyable, assignable but movable
 2 layer → _Promise – promise
 _Promise for generic, promise for...
future
 traits
 non copyable, assignable but movable
 asynchronous return object that holds a value :?
 get
 is valid...
promise – future
future
promise
get_future
get
_Promise
_State
_Get_Only_Once
_State_manager
valid – already set?
set_valu...
packaged_task
 traits
 asynchronous provider
 return the result of a call to a function object
 members
 _Promise
 g...
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...
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
 keyword...
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
Upcoming SlideShare
Loading in …5
×

Synchronizing concurrent threads

539 views

Published on

Synchronizing concurrent threads
C++ Concurrency in Action: Chapter 4

Published in: Engineering
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
539
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Synchronizing concurrent threads

  1. 1. SYNCHRONIZING CONCURRENT THREADS C++ Concurrency in Action:Chapter 4
  2. 2. contents  condition variable  async  promise  future  packaged_task  chrono  message passing
  3. 3. 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
  4. 4. 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);
  5. 5. 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 };
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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!
  11. 11. 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
  12. 12. promise – packaged_task packaged_task _Stateoperator () _Call_immediate get_future future get functor _Promise
  13. 13. 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; }
  14. 14. wait!  for  wait_for duration  until  wait_until time point  listing  sleep, wait, try_lock, …
  15. 15. chrono  brief  duration  time value with measurement unit  time point  since a specific point in time epoch  keywords  steady clock  can be adjusted
  16. 16. message passing  send signal inter-actor manager programmercustomer Request a product Send functional list
  17. 17. message passing manager worker customer request notify working servicing studying programmer programming (transition)
  18. 18. message passing  in this book  seeAppendix C  author’s message-passing framework
  19. 19. C++ Concurrency in Action:Chapter 4 Synchronizing concurrent threads End-of-File

×