Monday, June 30, 2008

OmniThreadLibrary Example #1: Beep, world!

image In my previous post, I promised to publish some examples of OTL usage. Here is the first one - a simplification of the Hello, World program. This example doesn't write anything to the screen, it just makes a noise.

Start by creating a new Delphi VCL application. Drop a button on the form and write OnClick handler.

procedure TfrmTestOTL.btnBeepClick(Sender: TObject);
begin
CreateTask(Beep, 'Beep').Run;
end;

This will create a threaded task with name 'Beep' and main method Beep. Then the code will create a new thread and start executing this task in the context of the newly created thread. That's all, folks!


To make the example fully functional, add OtlTask unit to the uses list and write the Beep method.

procedure TfrmTestOTL.Beep(task: IOmniTask);
begin
MessageBeep(MB_ICONEXCLAMATION);
end;

Run the program, click the button. It will beep!


If you don't believe that the code is executed in a secondary thread, place a breakpoint on the MessageBeep call and check the Thread Status window. It will clearly indicate that the breakpoint was triggered from a secondary thread.


image


This example is included in the OTL repository in folder tests/0_Beep.

OmniThreadLibrary - threading library for Delphi [WIP]

imageIn my $$$ line of programming, everything revolves around threads. Servers, multithreaded processing on clients, real time hardware control ... you name it. In all the years I've learned many dos and don'ts of multithreaded programming. I've also written few frameworks to simplify this job and most of them are already retired. Sometimes a potentially good approach turns out not to be so good at all ...

In the last year I've become more and more unhappy with my current framework and decided that it's time for something better, something simpler. Something built from scratch. This time I didn't start with the implementation but tried to visualize how I'd like to use my framework in real-life applications. After all, I had all those test cases lying around in form of my old applications using old framework. And so, the OmniThreadLibrary was born.

Currently OTL is in a very fluent alpha state. I'm already using it in my projects but some more important interfaces are changing from day to day. Still, you're invited to try it and to comment on its usefulness. If you can give me an example of real-world problem that can't be implemented using OTL, all the better. Maybe I'll be able to enhance the OTL to suite your purpose more.

OTL is living on the Google Code hosting and is BSD licensed. Current implementation only supports Delphi 2007 (although I'd been told that only small modifications are required to make it work with Delphi 2006) and unless I'm given a very convicting business case support for older Delphis won't be included. Let's look into future, not into past.

Some examples are included in the repository, but not all parts of the current infrastructure are explored. I'll post more on OTL and describe examples and OTL's inner workings on this blog in the next few days.

Monday, June 23, 2008

TDM Rerun #9: My Data Is Your Data

There is no multiprocess solution without data sharing. If you have to do something useful and need more than one application to do it, you need a way to share data between those applications.

- My Data Is Your Data, The Delphi Magazine 88, December 2002

In TDM #88 my synchronisation & sharing theme continued with an article on shared memory. The article first demonstrated how shared memory can be implemented with file mapping Win32 API and then proceeded with the description of a wrapper that simplified this operation. This wrapper also added some useful extensions, as are text, stream and array access to the shared memory.

GpSharedMemory is still alive and well and used in many projects. Since the 2002 article it gained resizable shared memory, shared stream support and shared linked list.

Links: article (PDF, 60 KB), source code (ZIP, 50 KB), current GpSharedMemory unit