Welcome back to the fourth part of Concurrent Programming in fsharp blog post series. In part-2, we just learned that Alt<'a> is a subclass of Job<'a>. In this blog post, we are doing to dive deep into this abstraction and learn what it brings to the table.
An Example Before diving into the definition of Alt<'a>, let's figure out why we need it in the first place.
Assume that we have a function delayedPrintn which prints a given message after n milliseconds
Two years back, I wrote a blog post on how to implement the API Gateway pattern using Reactive Extensions(Rx). In this third part of concurrent programming in fsharp using Hopac blog series, we are going to revisit that blog post and port it to use Hopac instead of Reactive Extensions.
Rx vs Hopac The critical difference between Rx and Hopac is their communication model.
Rx is primarily a system for querying data in motion asynchronously, and systems built using Rx rely on asynchronous message-passing.
In the last blog post, we learned the how to create jobs and run them parallelly using Hopac. In this second part of the blog post series, we are going to explore inter-job communication.
Share Memory By Communicating In multi-threaded programming model, if two threads want to communicate, the typical approach is using a shared memory backed by locks, thread-safe data structures (or other synchronisation primitives). We have to follow specific best practices to avoid Deadlocks and Race Conditions while using the shared memory approach.
Enabling developers to write concise code in solving complex problems is one of the significant characteristics of functional programming. The conciseness is mostly due to the abstractions provided by the functional programming language.
Can we apply these abstractions and write concurrent programs with ease?
We are going to find the answer to this question by writing concurrent programs in fsharp using the Hopac library.
What is Hopac Hopac is a fsharp library that provides a programming model inspired by John Reppy's Concurrent ML language.
One of the recommended guidelines in Domain Driven Design is modelling the domain ideas using the domain type (CustomerName, CustomerId) instead of using their corresponding primitive type (string, int). In fsharp, with the help of Single-Case Discriminated Union, we can follow this guideline with minimal effort.
While following this practice in one of my recent project in fsharp, I came across a compelling use case, and I used a lesser-known approach to solve the problem.