
When accessing the VCL from multiple threads at the same time: adopted from …\DEMOS\THREADS\THRDDEMO.DPR
Great question a while ago:
[WayBack] “Don’t access VCL from a background thread” – how to demo that? – Primož Gabrijelčič – Google+
For me, the ultimate way why not to access the VCL from a background thread is the precursor of the official threads demo that ships from Delphi 2 to Delphi XE6 in ...DEMOS\THREADS\THRDDEMO.DPR
. where you’d think the thread isolation would be in ...DEMOS\THREADS\ThSort.pas
but actually is in ...DEMOS\THREADS\SortThds.pas
.
The first public showing of that demo did not include main thread protection. It originates from a session at the the 1995 Borland Developers Conference where Ray Konopka showed the below code from Bob Ainsbury.
That session reminded why this joke [WayBack] Via the EKON20 sessions… – Jeroen Wiert Pluimers – Google+ was so funny: “When Ray Konopka enters the room you have a Raize condition.“.
The question above also made me find back this reference to BorCon95 in San Diego:
[WayBack] Interview with Ray Konopka by Clay Shannon: Ray Konopka talks about the mechanics of the Borland Conference advisory board, his work on Code Site for .NET, the great BorCon pizza thread time slice demonstration, his High School record in Track and Field, and Octane
<<
What was the funniest experience you’ve ever had related to programming?
At the Borland Conference in San Diego in 1995, Bob Ainsbury and I presented a session on multithreaded programming in Delphi. (Well, it was actually an alpha version of Delphi32.) About halfway through the session Bob starts talking about scheduling and says that maybe a demonstration would be helpful. Bob then selects three volunteers from the audiencetwo men and one woman.
With the three volunteers up at the front, Bob directs me to come out from behind the computer. At this point, I had absolutely no idea where all this was heading. Bob had asked me to help with his session that morning and while I worked on a big demonstration program for the session, he worked on the rest of his presentation. The point is that I did not see any of Bobs slides or other material until he showed them in the session.
Bob then tells me that I will be the scheduler and the three volunteers are threads. Bob then gives each person (i.e. thread) a priority level both men get the same priority value while the woman gets a slightly lower value.
Bob then brings out some slices of rather cold pizza and puts them on the table in front of the three threads. Bob then explains that each thread gets to do work when the scheduler gives the thread a time slice. In other words, when I point to a thread, that person is to pick up a pizza slice and take a bite.
Next, Bob tells me to schedule the first thread, so I point to one of the men and he takes a bite of pizza. Bob then tells me to switch to the next thread, so I point to the other man, and he takes a bite of pizza. Again, Bob tells me to switch threads, so I point back to the first man as the woman has a lower priority value, and the man takes another bite. At this point, the audience is really getting into it. Bob then has me switching threads more quickly and these two guys, who were great sports, kept taking bites of pizza. By now, everyone was laughing, including the woman who did not have to stuff her face full of pizza. Of course, in the end, Bob explained how this illustrated how a thread can be starved.
>>
At that conference, I made an epic stage appearance winning a beer bottle from a quiz question, but not being to open it. One of the first times in the USA for me, I didn’t know what a “twist off” was and my opener was in the hotel room (:
As Ray Konopka is still in the Delphi world with [WayBack] Raize Software, lots of people still know him. Some people [Archive.is] wondered whatever happened to Bob Ainsbury (he was already famous in the DOS era for his DOS 6 Secrets book and Turbo Vision era for his free “TechnoJocks Turbo Toolkit” and “TechnoJock’s Object Toolkit”, but moved on from the Delphi world a long time ago). He is @bainsbury and has a [Archive.is] broad belt of tools for his work.
Back to ...DEMOS\THREADS\THRDDEMO.DPR
:
I’m not sure why they removed the multi-threading demo, so since the heritage, I made it into a gist.
The trick is to remove the Synchronize
call in the VisualSwap
method below. It is the basic way of ensuring a parameterless executes in the main VCL thread:
- [WayBack]
unit classes; procedure TThread.Synchronize(AMethod: TThreadMethod); overload;
- [WayBack]
unit classes; TThreadMethod = procedure of object;
More simple demo to crash the VCL
In the end Primož went with this:
Found a simple one.
Create a new VCL program. Drop a TImage on the form. Insert this code:
procedure TForm47.FormCreate(Sender: TObject); begin TThread.CreateAnonymousThread( procedure begin PaintToImage(Image1); end).Start; end; procedure TForm47.PaintToImage(image: TImage); begin while true do begin Image1.Picture.Bitmap := TBitmap.Create; Image1.Picture.Bitmap.Free; end; end;Run in debugger. Instant access violation in the WMPaint handler of the main thread.
–jeroen