Thread affinity of user interface objects, part 4: GDI objects and other notes on affinity


GDI objects are much simpler. As a general rule, they all have process affinity: They can be used by any thread in the process that created them. If you use a GDI object from multiple threads, it is your responsibility to coordinate the object's use.

Note that the window manager and GDI as a general rule keep their respective objects thread-safe. When I say that it is your responsibility to coordinate an object's use from multiple threads, I mean that you have to coordinate among your own threads if you're going to modify the object from one thread and read from it on another or modify it from two threads. For example, if one thread enumerates a menu while another is modifying it, the one doing the enumeration will get inconsistent results. Similarly, if two threads both try to change a menu item at the same time, the last writer will win.

Next time, we wrap up with a discussion of clean-up.

Comments (4)
  1. mschaef says:

    I apologize if this is out of scope for blog comments on this post, but it seems related to thread affinity.

    Can HFONT’s be selected into more than one DC at a time? Or, is it essential to have a seperate HFONT for each DC?

  2. mschaef says:

    "related to thread affinity"

    Related in the sense of: "Must I enforce this constraint if multiple threads are using my HFONT? Or more generally, must I enforce this constraint at all?"

  3. Mike says:

    I don’t know about fonts, but I do know you need to either synchronize else duplicate (application-resource) bitmaps. I had a multithreaded MDI (test-) app that used the same bitmap in two different MDIChild windows running in separate threads. While it worked as expected on a uni-CPU machine it flickered madly on a dual box (read: it was a complete disaster).

    So if bitmaps can only be selected into a single DC (even if only for read-access, and even if the DC’s are 100% compatible – no difference in resolution, color-depth or anything), I see no obvious reason to expect fonts should be any better – except they aren’t usually application-resources, but system resources, so maybe the system serializes access to them (or even allows concurrent access).

  4. sandman says:

    Thanks , for these interesting comments on thread and

    process affinity.

    Having struggled without this informaion in the past, is there any chance that this information will get into the msdn documentation. At least in term of the defining the affinity scope of the objects and illegal behaviours.

    Thanks

Comments are closed.

Skip to main content