Waitgroups in Cilk
This Cilk code demonstrates the equivalent functionality of Go’s WaitGroups using Cilk’s built-in synchronization mechanisms. Here’s an explanation of the key differences and concepts:
We use
#include <cilk/cilk.h>
and#include <cilk/cilk_api.h>
to access Cilk’s functionality.Instead of goroutines, we use Cilk spawns with the
cilk_spawn
keyword.The
worker
function remains largely the same, but we use C++ standard library functions for I/O and sleep operations.In the main function, we don’t need to create an explicit WaitGroup. Cilk handles synchronization automatically.
We use a for loop to spawn multiple workers, similar to the Go example.
Instead of
wg.Wait()
, we usecilk_sync
to ensure all spawned tasks are completed before the program exits.Cilk doesn’t have a direct equivalent to Go’s defer statement, so we don’t need to worry about calling a “Done” function.
To compile and run this Cilk program:
The order of workers starting up and finishing is likely to be different for each invocation, just like in the original example.
Note that Cilk provides a simpler model for parallelism compared to explicit threading or Go’s goroutines. It automatically handles load balancing and work stealing, which can lead to efficient execution on multi-core systems.
For more advanced use cases, Cilk also provides additional synchronization primitives and parallel constructs, but for this simple example, cilk_spawn
and cilk_sync
are sufficient to demonstrate the concept of waiting for multiple parallel tasks to complete.