Just a short notice for Slovenian readers - In case you missed it, next week you'll be able to join a Slovenian webinar about Delphi 11.
Thursday, October 14, 2021
Wednesday, February 10, 2021
Readers-writer lock - Part 4: Improving TLightweightMREW
While the TLightweightMREW is quite handy, it is not perfect. There's a weird assymmetry in it. On all operating systems that Delphi can compile for, read locks are reentrant (recursive) while write locks are not. In other words, if a thread already owns a read lock, it can call BeginRead again and it will succeed. Write locks are different. If a thread already owns a write lock and calls BeginWrite again, it will either deadlock (on Windows) or raise an exception (on other supported platforms).
This is, however, relatively simple to fix. I have implemented a simple wrapper for the TLightweightMREW lock in TLightweightMREWEx. This new record uses internal TLightweightMREW to provide locking and adds some simple logic to implement write lock reentrancy. The implementation and accompanying test program rwReentrantWriter can be found at https://github.com/gabr42/examples/tree/master/Reader-writer%20lock.
Monday, February 08, 2021
Readers-writer lock - Part 3: Some numbers
In order to convince you that a readers-writer lock is not a stupid idea, I should finally show some numbers. In this article I'll present a minimalistic (but still real-life) example which allows us to compare different locking solutions.
All code from this article is available in project rwLock at https://github.com/gabr42/examples/tree/master/Reader-writer%20lock
Tuesday, December 08, 2020
Readers-writer lock - Part 2: Implementation
In the previous installment I introduced the idea of a readers-writer lock. Today I'll look into readers-writer lock implementations (yes, multiple) that are available in the Delphi run-time library.
Sunday, November 08, 2020
Readers-writer lock - Part 1: Why?
One of the pleasant surprises in Delphi 10.4.1 was the addition of a new readers-writer lock implementation TLightweightMREW. While it was probably not noticed by most of the users, I was quite happy to see it implemented.
So now you are asking yourself - what is this readers-writer lock and why am I so happy to see it in Delphi? Well, I'm glad that you're asking! Let me explain ...
In multithreaded programming (as most of my horror stories start), we frequently run into a problem of resource sharing. Two threads want to modify a shared resource at the same time and that can cause many problems, from information being overwritten to corrupted data and program crashes.
To fix this, we add resource protection. Usually that is just a critical section (typically through a TCriticalSection wrapper), or Delphi's TMonitor. Sometimes, however, protecting resources with a simple critical section causes an unnecessary performance drop, and that's when a readers-writer lock (may) come into play.
Saturday, May 30, 2020
OmniThreadLibrary 3.07.8
Version 3.07.8 is mostly a bugfix release. It fixes few small bugs and enables support for Delphi 10.4.
You can get it now on git, download the ZIP archive, install it with Delphinus or with GetIt (in few days).
For more information, visit OmniThreadLibrary home page or write your question on the forum.
Wednesday, May 27, 2020
Top three Delphi 10.4 features
Friday, February 14, 2020
Long live Delphi!
Friday, November 29, 2019
Sales! Sales! Sales!
Parallel Programming with OmniThreadLibrary
printed book: 25% off (use coupon BLACKFRIDAY25)
Delphi High Performance
Hands-on Design Patterns with Delphi
Friday, November 08, 2019
ITDevCon X
Monday, September 30, 2019
CompareValue for booleans
CompareValue function is incredibly practical when you are writing comparers (functions that determine how some data structure is ordered). System.Math and System.StrUtils define a bunch of functions that can be used to compare integers, doubles, strings … There’s, however, no CompareValue for booleans.
A CompareValue function compares two parameters, traditionally named left and right, and returns 0 if they are the same, –1 if left is smaller and 1 if right is smaller.
If we use the usual ordering of false < true, we can write the missing function as follows:
function CompareValue(left, right: boolean): integer; overload; begin if left < right then Result := -1 else if left > right then Result := 1 else Result := 0; end;
Your task for today – if you choose to accept it – is: Write this function without any if statements.
Thursday, September 12, 2019
Visit “What’s new in Rio 10.3.2” in Ljubljana
On September 26th I’ll talk about RAD Studio Rio in Ljubljana. We’ll discuss 10.3 a bit and 10.3.1/10.3.2 updates in more detail. We’ll also look into the future to see what 10.4 might bring.
This will also be a good opportunity to see my latest book, Design patterns with Delphi, or get your own copy signed.
Participation is free, but you should register here so we can prepare enough food for everyone.
Tuesday, July 16, 2019
When True is not
Writeln ( True); Magic;Writeln ( True);
… output this:?
TRUE FALSE
Simple!
Tuesday, July 02, 2019
The case of a missing begin/end
Did you know that this is a valid syntax?
case a of
0: Writeln(0);
else
Writeln('else');
Writeln(a);
end;
This code indeed compiles and works exactly as the following fragment.case a of
0: Writeln(0);
else begin
Writeln('else');
Writeln(a);
end;
end;
I personally would never drop begin/end inside a case/else statement, but at least someone must disagree. I found such example in a very (VERY!) old code (it was written for Delphi 2) and I was quite surprised that it compiles at all.EDIT
Anton Alisov suggested formatting first example as:
case a of
0: Writeln(0);
else
Writeln('else');
Writeln(a);
end;
I guess this makes more sense (but just an itsy bitsy teenie weenie bit more).Thursday, June 06, 2019
Monday, April 29, 2019
Spring4D European Conference 2019 sessions
Slides and code for the Spring4D conference are now published on the conference page.
Slides and code for my two sessions – Design patterns with Spring4D and Interception and dynamic proxy – are also available on my presentations page.
Saturday, April 27, 2019
FastMM4 large memory allocation–benchmarking VirtualAlloc
TLThere were, however, other interesting results that my simple benchmark pointed out. More on that in a moment, but first…; DR Yes, allocating from the top is slower. No, the difference is not big and in most cases you’ll not even notice it.
Thursday, April 18, 2019
Books, books, books
You probably know that I write books. A big part of making a book, however, is not just writing it, but letting all the potential readers out there know that the book exists. I'm doing a lot there - and so is my publisher - but still we can't reach all the potential readers ourselves.
That's where you come in!
If you have read any of my books and if you loved it and want the others to know, please consider publishing a review on the Amazon site. More reviews make Amazon algorithms treat the book with more respect and they recommend it more to customers.
Leave the review here: Delphi High Performance, Hands-on Design Patterns with Delphi.
You can also just tell your colleagues in any social circle - digital or real-life - that you like the book. Spread the word!
If you think that is too much and you don't want to get involved so deep, it doesn't matter. I still love you.
Thank you,
Primož
Monday, April 08, 2019
Deep Dive into Design Patterns
While writing Design Patterns with Delphi, I spent quite some time researching existing literature on design patterns implementation in Delphi, design patterns in other languages, other types of patterns, design principles and so on …
In case you would like to dig deeper than the book takes you, here is my reading list.
Tuesday, March 26, 2019
Spring conference, spring edition

It’s
You can join us for a day or two. Your call, but I would
“Bee there Orr Bee A Rectangular Thyng
”
- The Band With Rocks In *