Setting Vcl.Clipbrd.Clipboard.AsText content is *thread-safe*, because internally it calls the GlobalAlloc/GlobalUnlock statements in the SetBuffer method. Is it correct?

Setting Vcl.Clipbrd.Clipboard.AsText content is *thread-safe*, because internally it calls the GlobalAlloc/GlobalUnlock statements in the SetBuffer method. Is it correct?

Comments

  1. The GlobalXXX functions are of course threadsafe. But that's hardly the issue. The real issue is whether or not multiple threads in your processes, or multiple processes try to access the clipboard concurrently.

    The OpenClipboard function provides serialisation for that. So the question is which window handle is passed to OpenClipboard. Have a look at the code to find out.

    ReplyDelete
  2. making hard dependency upon implementation details is hiding a landmine for your successors to find.

    ReplyDelete
  3. Arioch The That's nonsense. Libraries can have contracts. If they don't they are useless.

    ReplyDelete
  4. David Heffernan the question does mention contract? I don't see it. I see that question is about the function (actually, procedure in Delphi terms) being thread-safe regardless of contract because of "internally it uses GlobalAlloc"

    ReplyDelete
  5. Arioch The Clearly GlobalAlloc has nothing to do with the question. But it doesn't mean that we can't understand the contract of AsText.

    ReplyDelete
  6. David Heffernan you could if you discuss the contract, but no one does, you discuss implementation.

    ReplyDelete
  7. David Heffernan I see from the VCL source code that Application.Handle is passed to the OpenClipboard API, but it's not very clear to me that what does it mean to the *thread safety*?

    ReplyDelete
  8. Arioch The That makes sense, the VCL's documentation says nothing about the clipboard accessing thread safety, so I'd rather setting the clipboard text in the main thread (which is possible in this specific case in question).

    ReplyDelete
  9. Edwin Yip it means that there is no thread safety

    ReplyDelete
  10. David Heffernan OIC, it means that multiple threads may access the same clipboard handle but has no guarding mechanism applied.
    Thank you all!

    ReplyDelete
  11. Edwin Yip you may also block your program over Clipboard object, but in heavily multithreaded program it can be slow (TMonitor, https://www.delphitools.info/2013/05/30/performance-issue-in-nextgen-arc-model/ )
    But if, chances are, your MainVCLThread might sometimes get stuck for long, that would seem a safer option. Maybe even make a thread-safe wrapper like TThreadList vs TList and inject it into Clipboard variable

    ReplyDelete

Post a Comment