#1 2014-03-02 00:33:18

RussB
Member
Registered: 2014-03-02
Posts: 2

Is SynGDIPlus Thread-Safe?

Hello,

I have a Delphi XE app that has a

Gdip := TGDIPlusFull.Create('gdiplus.dll');

in the main UI thread.  I then create several threads that read jpg files using SynGDIPlus and converts them to .bmps.  Here is the thread.execute pseudo-code:

procedure ttcTThumbnailDBThread.Execute;
var
  g: TSynPicture;  //this is declared as a local variable to the thread .execute 
  ams: TMemoryStream;
  Src: TBitmap;
begin
  Ams := TMemoryStream.Create;
  Ams.LoadFromFile("a jpg file");
  g := TSynPicture.Create;
  g.LoadFromStream(Ams);
  Src := g.ToBitmap;
  --do something with the resulting bitmap that is now in SRC 
  G.Free;
  Ams.Free;
  Src.Free;
end;

Is the g.LoadFromStream and g.ToBitmap code thread-safe? Microsoft mentions that you should Synchronize the GDI+ object. 

But, in the case of SynGDIPlus, is the object the 'g: TSynPicture' declaration, or is it the Gdip := TGDIPlusFull.Create('gdiplus.dll') declaration?

I guess I am confused about whether the 'g' or 'Gdip' is the actual GDI+ object.

Anyone have any ideas?  Do I need to synchronize all calls to GDI+?

-------

The only thing I can find on this is from

http://msdn.microsoft.com/en-us/library … ip_threads

Thread Synchronization (from the URL above)
It is possible for more than one thread to have access to a single GDI+ object. However, GDI+ does not provide any automatic synchronization mechanism. So if two threads in your application have a pointer to the same GDI+ object, it is your responsibility to synchronize access to that object.
Some GDI+ methods return ObjectBusy if a thread attempts to call a method while another thread is executing a method on the same object. Do not try to synchronize access to an object based on the ObjectBusy return value. Instead, each time you access a member or call a method of the object, place the call inside a critical section, or use some other standard synchronization technique.

----

Cheers,

Russ

Last edited by RussB (2014-03-02 00:34:42)

Offline

#2 2014-03-02 16:57:34

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Is SynGDIPlus Thread-Safe?

If your GDI+ objects are not accessed concurrently among the threads, it sounds pretty safe to work with.

So in your case, each thread reading the JPG will have its own TSynPicture object.
As such, it is thread-safe.

The TGdiPlusFull instance does not contain nor create any GDI+ object, just the pointers to gdiplus.dll functions, and some global parameters.

IMHO you do not need to synchronize calls to GDI+, as soon as you do not have concurrent access to the same GDI+ object.
And in this case, do not use "Synchronize", but Critical Sections to protect the resources.

Offline

#3 2014-03-05 07:59:34

RussB
Member
Registered: 2014-03-02
Posts: 2

Re: Is SynGDIPlus Thread-Safe?

It is good to hear that it should work.  Many thanks for this library!

Russ

Offline

#4 2018-09-06 05:18:31

BugsDigger
Member
Registered: 2018-09-06
Posts: 2

Re: Is SynGDIPlus Thread-Safe?

See my post

Some methods of SynGdiPlus are not thread-safe
https://synopse.info/forum/viewtopic.php?id=4689

Offline

Board footer

Powered by FluxBB