Sterling has too many projects Blogging about programming, microcontrollers & electronics, 3D printing, and whatever else...

» Parallel Loop Execution

Iteration is slow. If you have N things to process in a loop, your loop will take N iterations to process. Slow. Sometimes that’s the only way, though, to solve a problem. For example, let’s consider the case where we have a JSON log and we want a command to read each line, parse the JSON for that log, and summarize it showing the time stamp and message: use JSON::Fast; my $log-file = 'myapp.
» read more

» Breaking Down Concurrency Problems

The goal of today’s article is to consider when you want to run your tasks simultaneously and how to do that. I am not going to give any rules for this because what works one time may not work the next. Instead, I will focus on sharing some guidelines that I have learned from personal experience. Remember Your Promises Whenever you use concurrency, you want to hold on to the related Promise objects.
» read more

» Thread Safe Structures Without Locking

As I have said several times before in this calendar, it is always best to avoid sharing state between running threads. Again, however, here is yet another way to share state, when you need to do it. A few days ago, we considered monitors as a mechanism for creating a thread-safe object. Let’s consider the following monitor: class BankBalanceMonitor { has UInt $.balance = 1000; has Lock $!lock .= new; method deposit(UInt:D $amount) { $!
» read more

» Supply Back Pressure

In Raku, a Supply is one of the primary tools for sending messages between threads. From the way a Supply is structured, it is obvious that is provides a means for one ore more tasks to send events to multiple recipient tasks. What is less obvious, however, is that a Supply imposes a cost on the sender. Consider this program: my $counter = Supplier.new; start react whenever $counter.Supply { say "A pre-whenever $_"; sleep rand; say "A post-whenever $_"; } start react whenever $counter.
» read more

» Comparing react with tap

In Raku we have a couple basic ways of getting at the events emitted from a Supply, which begs the question, what’s the difference between each? I want to answer that question by creating a react block with a couple intervals and then emulate the same basic functionality using tap. Let’s start with our base react block: sub seconds { state $base = now; now - $base } react { say "REACT 1: {seconds}"; whenever Supply.
» read more

» Semaphores

A semaphore is a system of sending messages using flags. Oh wait, that’s what a semaphore is outside of computing. Among computers, a semaphore is like a kind of lock that locks after being acquired N times. This is useful for situations where you have a resource of N items, want to quickly distribute them when you know they are available, and then immediately block until a resource has been released.
» read more

» Breaking Down Async Problems

One challenge I’ve often faced when writing asynchronous code is trying to figure out how to break the problem down in a reasonable way. How far do I go in breaking up my code? How many steps do I want to take? How do I deal with tasks that branch or fork? How do I deal with the interdependencies I’ve created. My hope in this article is to give some guidelines I’ve learned and some tools for answering these questions.
» read more

» Compare-and-swap Your Scalars

Previously, I discussed the compare-and-swap operation as an operation to perform on atomicint variables. That’s just the tip of the iceberg. While most of the atomic emoji ⚛️ operators are only for atomicints, the cas function, atomic-fetch (or prefix ⚛️ operator), and atomic-assign (or ⚛️= operator) can all be used on any kind of Scalar variable. First, we need to make sure we know what a Scalar is. In Raku, every variable name is associated with a container.
» read more

» The Monitor Pattern

Today I want to discuss the use of locks to make an object thread safe. That is, by employing a simple pattern for locking access to your object, you can effectively guarantee that only a single thread has access to any part of the object at a time. This, therefore, guarantees that the state of the object can never be corrupted, even when multiple threads attempt to access it concurrently.
» read more

» Keep Your Thread or Don't

In Raku, there are different ways to pause your code. The simplest and most obvious way to do so is to use sleep: my $before = now; sleep 1; my $after = now - $before; say $after; Assuming your system is not bogged down at the moment, the output of $after should be a number pretty close to 1. Not very exciting: you didn’t do anything for a second. Woo. Woo.
» read more