Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

XebiCon'17 : Communicating sequential processes - Diana Ortega

148 vues

Publié le

Si nous considérons un logiciel comme un ensemble de tâches qui sont exécutées par des processus, le défi de créer des logiciels scalables nous amène aux questions de la gestion de la concurrence et de la communication entre processus. Le modèle traditionnel de threads utilise des espaces partagés de mémoire pour communiquer et des mutex ou des sémaphores pour se synchroniser. Cependant, à grande échelle, ce modèle reste très difficile à maintenir.
Et si au lieu de partager la mémoire, nous synchronisions les processus via l’échange de messages ? Cette idée n’est pas nouvelle et constitue même le fondement du Modèle d’Acteurs et du Communicating Sequential Processes (CSP). D’ailleurs, saviez-vous que les routines du langage Go, le framework Akka et le langage Erlang sont en fait des implémentations de ces modèles ?
Si vous voulez en savoir plus, venez voir les bases de la construction d’applications hautement scalables avec des exemples d’implémentation en Go, Akka et Elixir.

Publié dans : Technologie
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

XebiCon'17 : Communicating sequential processes - Diana Ortega

  1. 1. Diana ORTEGA @dicaormu
  2. 2. Paper and Book Charles Antony Richard Hoare “A paradigm for expressing concurrency based on message-passing”
  3. 3. Texte ici Communicating Sequential Processes Agenda ● Definitions : concurrency, parallelism, etc ● How do we usually deal with concurrency? ● Communicating Sequential Processes (CSP) and Actors ● Conclusion and takeaways
  4. 4. Go!
  5. 5. Communicating Sequential Processes Definitions Process
  6. 6. Communicating Sequential Processes Definitions Process ● Behaviour pattern of an object ● Implemented by functions ● Actions Object behaviour()
  7. 7. Communicating Sequential Processes Definitions Process ● Behaviour pattern of an object ● Implemented by functions ● Actions http://www.designmethodsandprocesses.co.uk/ public int function(int x){ System.out.println("hello"); ….. return 1; }
  8. 8. Communicating Sequential Processes Definitions Process ● Behaviour pattern of an object ● Implemented by functions ● Actions
  9. 9. Texte ici Concurrency vs Parallelism Communicating Sequential Processes Definitions
  10. 10. Parallelism in real life
  11. 11. Texte ici Time Process Process Process Time Process Process Process Process Concurrency enables parallelism Definitions: Concurrency vs Parallelism Communicating Sequential Processes
  12. 12. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  13. 13. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  14. 14. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  15. 15. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  16. 16. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  17. 17. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  18. 18. Texte ici Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  19. 19. Texte ici ● How do I write in the same space of memory? ● What if a process crashes and it has a lock? ● Where do I locate this space? Process 1 Process 2 Communicating Sequential Processes Interaction sharing memory
  20. 20. Texte ici Communicating Sequential Processes Interaction sharing memory Mutex Locks Semaphores Threads
  21. 21. Texte ici Interaction sharing memory ● Callback hell ● Fragmented logic ● How do we converge when we need to handle state?
  22. 22. Texte ici Communicating Sequential Processes Interaction sharing memory
  23. 23. Deadlocks or race conditions Foto: http://revistacorrientes.com/
  24. 24. Texte ici ● CSP ● Actor model ● Others (Amorphous computing, Flow-based programming) Interaction by message passing Communicating Sequential Processes
  25. 25. Texte ici Reasoning ● Look around you, What do you see? ● A complex world interacting, with independently behaving pieces Communicating Sequential Processes
  26. 26. Definitions: Concurrent process
  27. 27. Texte ici Communicating Sequential Processes CSP (original) Coroutines Subroutines
  28. 28. Texte ici Communicating Sequential Processes CSP (original) Coroutines Subroutines Processes
  29. 29. Texte ici Communicating Sequential Processes CSP (original) Coroutine A Coroutine B Communication by process name Communication by port subroutine C
  30. 30. Texte ici Communicating Sequential Processes CSP (original) Coroutine A Coroutine B Communication by process name Communication by port subroutine C syntax?
  31. 31. Texte ici<input_command>::=<source>?<target_variable> <output_command>::=<destination>!<expression> <source>::=<process_name> <destination>::=<process_name> <process_name>::=<identifier>|<identifier>(<subscripts>) <subscripts>::=<integer_expression>,{<integer_expression>} <target_variable> :.---<simple_variable>l<structured_target> <structured_target> :.---<constructor>(<target_variable_list>) <target_variable_list> :~ <empty>[<target_variable> {,<target_variable>} Communicating Sequential Processes Csp: communication
  32. 32. Texte ici<input_command>::=<source>?<target_variable> <output_command>::=<destination>!<expression> <source>::=<process_name> <destination>::=<process_name> <process_name>::=<identifier>|<identifier>(<subscripts>) <subscripts>::=<integer_expression>,{<integer_expression>} <target_variable> :.---<simple_variable>l<structured_target> <structured_target> :.---<constructor>(<target_variable_list>) <target_variable_list> :~ <empty>[<target_variable> {,<target_variable>} Communicating Sequential Processes Csp: communication
  33. 33. Texte ici<input_command>::=<source>?<target_variable> <output_command>::=<destination>!<expression> <source>::=<process_name> <destination>::=<process_name> <process_name>::=<identifier>|<identifier>(<subscripts>) <subscripts>::=<integer_expression>,{<integer_expression>} <target_variable> :.---<simple_variable>l<structured_target> <structured_target> :.---<constructor>(<target_variable_list>) <target_variable_list> :~ <empty>[<target_variable> {,<target_variable>} Communicating Sequential Processes Csp: communication
  34. 34. Texte ici<input_command>::=<source>?<target_variable> <output_command>::=<destination>!<expression> <source>::=<process_name> <destination>::=<process_name> <process_name>::=<identifier>|<identifier>(<subscripts>) <subscripts>::=<integer_expression>,{<integer_expression>} <target_variable> :.---<simple_variable>l<structured_target> <structured_target> :.---<constructor>(<target_variable_list>) <target_variable_list> :~ <empty>[<target_variable> {,<target_variable>} Communicating Sequential Processes Csp: communication
  35. 35. Texte ici αc(P)={v |c.v ∈αP} Communicating Sequential Processes What does CSP say?
  36. 36. Texte ici αc(P)={v |c.v ∈αP} Communicating Sequential Processes What does CSP say? ProcessProcess
  37. 37. Texte ici αc(P)={v |c.v ∈αP} Communicating Sequential Processes What does CSP say? Value of message
  38. 38. Texte ici αc(P)={v |c.v ∈αP} Communicating Sequential Processes What does CSP say? communication event
  39. 39. Texte ici αc(P)={v |c.v ∈αP} Communicating Sequential Processes What does CSP say? Channels have type
  40. 40. Texte ici Process1 Process2 Communicating Sequential Processes CSP
  41. 41. Texte ici Process1 Process2 Communicating Sequential Processes CSP Channel
  42. 42. Texte ici Process1 Process2 Communicating Sequential Processes CSP Channel
  43. 43. Texte ici “The Actor model adopts the philosophy that everything is an actor. ” Wikipedia Communicating Sequential Processes Actor model
  44. 44. Texte ici Actor1 Actor2 Communicating Sequential Processes Actor model
  45. 45. Texte ici Actor1 Actor2 MailBox MailBox Communicating Sequential Processes Actor model
  46. 46. Texte ici Actor1 Actor2 MailBox MailBox Communicating Sequential Processes Actor model
  47. 47. Texte iciActor System Address Address Address Communicating Sequential Processes ● processing ● storage ● communication Actors
  48. 48. Texte ici Communicating Sequential Processes CSP and Actors Program
  49. 49. Texte ici Communicating Sequential Processes CSP and Actors Process Process Process Process
  50. 50. Texte ici Communicating Sequential Processes CSP and Actors Process1 Process2 Process3 Process4 I speak CSP!! me too!! :D I’m an Actor me too!
  51. 51. Texte ici Sending first
  52. 52. Texte ici Time Time CSP Actors Communicating Sequential Processes CSP and Actors
  53. 53. Texte ici Time Time Process1 writing Actor writing Communicating Sequential Processes CSP and Actors
  54. 54. Texte ici Time Time Process1 writing Actors Communicating Sequential Processes CSP and Actors Process1 waiting
  55. 55. Texte ici Time Time Process2 reading Actors Communicating Sequential Processes CSP and Actors Process1 writing Process1 waiting
  56. 56. Texte ici Time Time Actors Communicating Sequential Processes CSP and Actors Process2 reading
  57. 57. Texte ici Reading first
  58. 58. Texte ici Time Time Process1 waiting Communicating Sequential Processes CSP and Actors Actor1 waiting
  59. 59. Texte ici Time Time Process1 waiting Communicating Sequential Processes CSP and Actors Actor1 waiting Process2 Writing Actor2 Writing
  60. 60. Texte ici Time Time Process1 waiting Communicating Sequential Processes CSP and Actors Actor1 waiting Process2 Writing Actor2 Writing Process1 reading Actor1 reading
  61. 61. Texte ici Time Time Process1 waiting Communicating Sequential Processes CSP and Actors Actor1 waiting Process2 Writing Actor2 Writing Process1 reading Actor1 reading Process1 Processing Actor1 processing
  62. 62. Texte ici Time Time Process1 waiting Communicating Sequential Processes CSP and Actors Actor1 waiting Process2 Writing Actor2 Writing Process1 reading Actor1 reading Process1 Processing Actor1 waiting
  63. 63. Texte ici The sieve of Eratosthenes. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Communicating Sequential Processes Example
  64. 64. Texte ici The sieve of Eratosthenes. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 p =2 look for multiples starting by p^2 Communicating Sequential Processes Example
  65. 65. Texte ici The sieve of Eratosthenes. 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 p =3 look for multiples starting by p^2 Communicating Sequential Processes Example
  66. 66. Texte ici The sieve of Eratosthenes. 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55 59 61 65 67 71 73 77 79 83 85 89 91 95 97 p =5 look for multiples starting by p^2 Communicating Sequential Processes Example
  67. 67. Texte ici The sieve of Eratosthenes. 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97 p =7 look for multiples starting by p^2 Communicating Sequential Processes Example
  68. 68. Texte ici The sieve of Eratosthenes. 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 p =11 look for multiples starting by p^2 Communicating Sequential Processes Example
  69. 69. Texte ici Communicating Sequential Processes Solution CSP
  70. 70. func main() { ch := make(chan int) // Create a new channel. go Generate(ch, 100) // Launch Generate goroutine. Sieve(ch) } func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) }
  71. 71. func Generate(ch chan<- int, number int) { for i := 2; i < number; i++ { ch <- i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } }
  72. 72. Texte ici func main() { ch := make(chan int) // Create a new channel. go Generate(ch, 100) // Launch Generate goroutine. Sieve(ch) } ch: channel int Communicating Sequential Processes Explanation A channel in Go provides a connection between two goroutines, allowing them to communicate.
  73. 73. Texte ici func main() { ch := make(chan int) // Create a new channel. go Generate(ch, 100) // Launch Generate goroutine. Sieve(ch) } stack Generate Communicating Sequential Processes Explanation ch: channel int It's an independently executing function, launched by a go statement.
  74. 74. Texte ici func Generate(ch chan<- int, number int) { for i := 2; i < number; i++ { ch <- i // Send 'i' to channel 'ch'. } } Communicating Sequential Processes Explanation stack Generate number := 100 i:=2 ch: channel int i 2 Synchronisation, it waits for a receiver to be ready.
  75. 75. Texte ici func main() { ch := make(chan int) // Create a new channel. go Generate(ch, 100) // Launch Generate goroutine. Sieve(ch) } Communicating Sequential Processes Explanation stack Generate ch: channel int 2
  76. 76. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } prime :=2 Communicating Sequential Processes Explanation stack Generate ch: channel int 2 synchronization, it will wait for a value to be sent
  77. 77. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } Communicating Sequential Processes Explanation stack Generate ch: channel int ch1: channel int
  78. 78. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } stack Filter Communicating Sequential Processes Explanation stack Generate ch: channel int ch1: channel int Channels both communicate and synchronize
  79. 79. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } Communicating Sequential Processes Explanation stack Filterstack Generate ch: channel int ch1: channel int
  80. 80. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } prime :=... Communicating Sequential Processes Explanation stack Filterstack Generate ch: channel int ch1 (new ch): channel int
  81. 81. Texte ici func Generate(ch chan<- int, number int) { for i := 2; i < number; i++ { ch <- i // Send 'i' to channel 'ch'. } } Communicating Sequential Processes Explanation prime :=... stack Filterstack Generate number := 100 i:=3 ch: channel int ch1 (new ch): channel int i 3
  82. 82. Texte ici func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } } Communicating Sequential Processes prime :=... stack Filter prime:=2 stack Generate ch(now in): channel int ch1 (now out): channel int i:=33 Explanation A sender and receiver must both be ready to play their part in the communication. Otherwise we wait until they are
  83. 83. Texte ici func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } } Communicating Sequential Processes Explanation prime :=... stack Filter prime:=2 stack Generate ch(now in): channel int ch1 (now out): channel int i:=3 3
  84. 84. Texte ici func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } prime := 3 Communicating Sequential Processes Explanation stack Filterstack Generate ch: channel int ch1 (new ch): channel int 3
  85. 85. Texte ici func Generate(ch chan<- int, number int) { for i := 2; i < number; i++ { ch <- i // Send 'i' to channel 'ch'. } } Communicating Sequential Processes Explanation stack Filterstack Generate number := 100 i:=4 ch: channel int ch1: channel int i 4
  86. 86. Texte ici func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } } Communicating Sequential Processes Explanation stack Filter prime:=2 stack Generate ch(now in): channel int ch1(now out): channel int i:=4 4
  87. 87. Texte ici func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } } Communicating Sequential Processes Explanation stack Filter prime:=2 stack Generate ch(now in): channel int ch1(now out): channel int i:=4
  88. 88. Texte ici And so on….. Communicating Sequential Processes Explanation
  89. 89. Texte ici Result 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
  90. 90. Texte ici Communicating Sequential Processes Solution Actor system
  91. 91. Texte ici implicit val system = ActorSystem("EratosthenesSystem") implicit val ec = system.dispatcher Communicating Sequential Processes Explanation
  92. 92. Texte ici val upper = 100 val eventualPrimes = PrimeFinderCalculator(upper) eventualPrimes.onComplete { case Success(primes) => println(primes) system.terminate() case Failure(error) => Console.err.println(error) system.terminate() } Communicating Sequential Processes Explanation
  93. 93. Texte ici object PrimeFinderCalculator { def apply(upper: Int, nrOfWorkers: Int = 2) (implicit system: ActorSystem, timeout: Timeout): Future[List[Int]] = { val master = system.actorOf(Master.props(upper), name = Master.name) system.actorOf(Eratosthenes.props(), name = Eratosthenes.name) (master ? Master.Messages.FindPrimes).mapTo[List[Int]] } } } Communicating Sequential Processes Explanation
  94. 94. Texte iciActor System /root /user /Eratosthenes:era tosthenes /Master: master Communicating Sequential Processes Explanation
  95. 95. Texte ici object PrimeFinderCalculator { def apply(upper: Int, nrOfWorkers: Int = 2) (implicit system: ActorSystem, timeout: Timeout): Future[List[Int]] = { val master = system.actorOf(Master.props(upper), name = Master.name) system.actorOf(Eratosthenes.props(), name = Eratosthenes.name) (master ? Master.Messages.FindPrimes).mapTo[List[Int]] } } } Communicating Sequential Processes Explanation
  96. 96. Texte iciActor System /root /user /Eratosthenes:era tosthenes /Master: master Communicating Sequential Processes findPrimes Explanation
  97. 97. Texte ici class Master(upper: Int) extends Actor { var client: Option[ActorRef] = None def receive: Receive = { case FindPrimes => client = Some(sender) val sieveRef = context.actorSelection(s"/user/${Eratosthenes.name}") sieveRef ! Eratosthenes.Messages.Sieve(self, Nil, (2 to upper).toList) case Result(list) => client.foreach(_ ! list) } } Messages are not anonymous Communicating Sequential Processes Explanation
  98. 98. Texte ici class Master(upper: Int) extends Actor { var client: Option[ActorRef] = None def receive: Receive = { case FindPrimes => client = Some(sender) val sieveRef = context.actorSelection(s"/user/${Eratosthenes.name}") sieveRef ! Eratosthenes.Messages.Sieve(self, Nil, (2 to upper).toList) case Result(list) => client.foreach(_ ! list) } } I know all actors in the system. I can look for an actor in a path and send messages Communicating Sequential Processes Explanation
  99. 99. Texte iciActor System /root /user /Eratosthenes:era tosthenes /Master: master Communicating Sequential Processes Sieve(self,nil, [2,3,..]) Explanation
  100. 100. Texte ici class Eratosthenes extends Actor with ActorLogging { def receive: Receive = { case Sieve(parent, primes, remaining) => remaining match { case Nil => parent ! Master.Messages.Result(primes) case h :: tail => self ! Sieve(parent, primes ++ List(h), tail.filter { x => x % h != 0 }) } } } Send message to itself with remaining list Explanation
  101. 101. Texte iciActor System /root /user /Eratosthenes:era tosthenes /Master: master Communicating Sequential Processes Sieve(parent,[2], [3,5..]) Explanation
  102. 102. Texte ici class Eratosthenes extends Actor with ActorLogging { def receive: Receive = { case Sieve(parent, primes, remaining) => remaining match { case Nil => parent ! Master.Messages.Result(primes) case h :: tail => self ! Sieve(parent, primes ++ List(h), tail.filter { x => x % h != 0 }) } } } send primes list to parent Explanation
  103. 103. Texte iciActor System /root /user /Eratosthenes:era tosthenes /Master: master Communicating Sequential Processes Result([2,3,5,7,...]) Explanation
  104. 104. Texte ici class Master(upper: Int) extends Actor { var client: Option[ActorRef] = None def receive: Receive = { case FindPrimes => client = Some(sender) val sieveRef = context.actorSelection(s"/user/${Eratosthenes.name}") sieveRef ! Eratosthenes.Messages.Sieve(self, Nil, (2 to upper).toList) case Result(list) => client.foreach(_ ! list) } } send response Communicating Sequential Processes Explanation
  105. 105. Texte ici Communicating Sequential Processes Result 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
  106. 106. Texte ici Building a small actor system using channels Mixing metaphors Process1 Actor1 Channel
  107. 107. Texte ici Building a small actor system using channels Mixing metaphors Actor1Process1Channel Actors are directly adressable. Channels are anonymous
  108. 108. Texte ici Building a small actor system using channels Mixing metaphors Mailboxes are only addressable by owning actor Actor1 Actor2 Mailbox actor1 Mailbox actor2
  109. 109. Texte ici Building a small actor system using channels Mixing metaphors Channels have type Process1Channel (Int)
  110. 110. Texte ici Building a small actor system using channels Mixing metaphors In actor model, only receiver blocks Process2ChannelProcess1
  111. 111. Texte ici ● communicating haskell processes ● go ● Clojure ● Javascript(js-csp) Communicating Sequential Processes Who does csp?
  112. 112. Texte ici ● erlang ● elixir ● Akka Communicating Sequential Processes Who does actors?
  113. 113. Texte ici Takeaways Communicating Sequential Processes Concurrent programming involves at least three distinct concerns: concurrency, mutual exclusion, and synchronisation
  114. 114. Texte ici Takeaways Communicating Sequential Processes Use the right tool for the job
  115. 115. Texte ici Takeaways Communicating Sequential Processes Actors and channels are often confused, we saw a mailbox implementation through channels to characterize them and “live” the differences
  116. 116. func main() { ch := make(chan int) // Create a new channel. go Generate(ch, 100) // Launch Generate goroutine. Sieve(ch) } func Sieve(ch chan int) { prime := <-ch print(prime, "n") ch1 := make(chan int) go Filter(ch, ch1, prime) Sieve(ch1) } A channel in Go provides a connection between two goroutines, allowing them to communicate. It's an independently executing function, launched by a go statement. synchronization, it will wait for a value to be sent Channels both communicate and synchronize channel int stack Generate
  117. 117. func Generate(ch chan<- int, number int) { for i := 2; i < number; i++ { ch <- i // Send 'i' to channel 'ch'. } } // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in <-chan int, out chan<- int, prime int) { for { i := <-in // Receive value from 'in'. if i%prime != 0 { out <- i // Send 'i' to 'out'. } } } Synchronisation, it waits for a receiver to be ready. A sender and receiver must both be ready to play their part in the communication. Otherwise we wait until they are
  118. 118. Texte ici Building a small actor system using channels Mixing metaphors Message ordering
  119. 119. Texte ici Building a small actor system using channels Mixing metaphors type Actor struct { …... mailbox chan interface{} } func (actor *Actor) doSomething(message interface{}) { select { case actor.mailbox <- message: default: //throw message away } } func (actor *Actor) receive(){ for{ message := <- actor.mailbox //deal with action …... } }
  120. 120. Texte ici How many messages should we wait? Mixing metaphors 1 message, the sender could block ∞ is not possible n=?
  121. 121. Texte ici Communicating Sequential Processes Building a channel using Actors
  122. 122. defmodule Chan.Chan do ... def loop do receive do {msg,caller} -> cond do is_integer(msg) -> Kernel.send(caller, msg) true -> loop() end end end … end test "echo" do {:ok, pid} = Chan.start_link() Chan.send(pid, 1) assert_receive 1 Chan.send(pid, :hi) refute_receive :hi end
  123. 123. Texte ici Building a channel using Actors Building a channel using Actors defmodule Chan.Chan do ... def loop do receive do {msg,caller} -> cond do is_integer(msg) -> Kernel.send(caller, msg) true -> loop() end end end … end

×