Concurrent atomic operations in C++ and Raku

Here’s a problem to solve: you have two threads, each incrementing the same single counter N times. What is the state of the counter at the end of the program? A straightforward solution A naïve C++ program can be written using the standard library threads like this: #include <iostream> #include <thread> int counter; void f() {     for (int c = 0; c != 100000; c++) counter++; } int main() {     std::thread thread_a {f}; … Continue reading “Concurrent atomic operations in C++ and Raku”

📘 Parallel file processing in Perl 6

Process the files from the current directory in a few parallel threads. We have to do something with each file in the directory, and it has to be done in such a way that files are processed independently with a few workers. It is not possible to predict how long the process will take for … Continue reading “📘 Parallel file processing in Perl 6”

📘 Atomic operations in Perl 6

Using the solution for Task 38, the Monte Carlo method, create a program that calculates the result using multiple threads. Perl 6 has built-in support for parallel computing. In Task 92, Sleep Sort, we’ve seen how to use the keywords await, gather, and take to spawn a few threads and wait for them to finish.  When different threads want to … Continue reading “📘 Atomic operations in Perl 6”

📘 Setting timeouts in Perl 6

Do not wait for a slow code block if it takes too long. In Perl 6, promises are the best way to create timeouts. In the following example, two code blocks are created; they are executed in parallel. my $timeout = Promise.in(2).then({    say ‘Timeout after 2 seconds’;});my $code = start {    sleep 5;    say ‘Done after 5 seconds’;} … Continue reading “📘 Setting timeouts in Perl 6”

📘 Channels in Perl 6

Perl 6 includes a number of solutions for parallel and concurrent calculations. The great thing is that this is already built-in into the language and no external libraries are required. The idea of the channels is simple. You create a channel through which you can read and write. It is a kind of a pipe … Continue reading “📘 Channels in Perl 6”

📘 The start keyword in Perl 6 promises

The start method creates a promise containing a block of code. There is an alternative way to create a promise by calling Promise.start via the start keyword. my $p = start {     42 } (Note that in Perl 6, a semicolon is assumed after a closing brace at the end of a line.) The … Continue reading “📘 The start keyword in Perl 6 promises”

🔬71. Implementing Int.sleep() in Perl 6

Hello! Yesterday, I was giving my Perl 6 Intro course at the German Perl Workshop in Gummersbash. It was a great pleasure to prepare and run this one-day course, and, while it was difficult to cover everything, we touched all main aspects of the Perl 6 language: from variables to regexes and parallel computing. Of … Continue reading “🔬71. Implementing Int.sleep() in Perl 6”

🦋 66. Atomic operations in Perl 6

N. B. The examples below require a fresh Rakudo compiler, at least of the version 2017.09. Discussing parallel computing earlier or later leads to solving race conditions. Let us look at a simple counter that is incremented by two parallel threads: my $c = 0; await do for 1..10 { start { $c++ for 1 … Continue reading “🦋 66. Atomic operations in Perl 6”