This blog has moved. Please update your bookmarks.

Thoughts About Threading

Threading, as I've mentioned before, is hard. Especially in non-managed languages, such as Delphi for Win32.

Various people, including Allen Bauer, is thinking about how languages can be adapted to support thread-based development easier:
"This increases the need for the average developer to be become more adept at writing multi-threaded applications. What if the compiler and the libraries made this easier and more accessible? It is these kinds of questions we ask ourselves everyday..."

One way of doing it, I'm thinking, is to have the compiler automatically generate warnings or errors in certain cases. We've patched the ZeosLib library so different threads cannot access ZConnection objects created in other threads; the ZConnection objects keep track of thread ownership and refuse to work for other threads (unless manually overriden). This behavior causes exceptions to be thrown in this case, which is far more desirable than having access violations deep inside one's code.

Just tossing ideas around, why not have certain attributes (I'm inspired by C#) that specify thread access? Like, for instance,
procedure TForm1.Log(const Text: string); mainthread;
... main-thread code here ...

Certain attributes might specify whether, like in this case, a method may only be accessed from a main thread; or with another attribute "threaded", special vigilance may be required and certain patterns may be enforced by the compiler. A function marked as "thread-unsafe" may not be called from a "threaded" method without certain encapsulation or something.

I think there are lots of things that can be done. Delphi/Win32 right now is at the same level that C++ were with strings (back when C++ was actually used by people). It can be done, but it's all manual, hard labor. Even few changes can yield significant returns.


Post a Comment

<< Home


Blog contents copyright © 2005 Mats Gefvert. All rights reserved.