Recent

Author Topic: [SOLVED] Thread synchronisation not going to plan...  (Read 345 times)

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1222
  • Former Delphi 1-7, 10.2 User
[SOLVED] Thread synchronisation not going to plan...
« on: December 02, 2020, 08:49:18 am »
I'm working on example code to retrieve a web page using NSURLSession custom delegates instead of the deprecated NSURLConnection.

In my custom delegate TMyDelegate.URLSession_task_didCompleteWithError(session: NSURLSession; task: NSURLSessionTask; error: NSError) procedure I inserted RTLEventSetEvent(ThreadDidFinish) and in the main thread I have RTLEventWaitFor(ThreadDidFinish) before attempting to display the retrieved data.

Alas, this causes the application to crash with this macOS error:

*** -[NSAutoreleasePool release]: This pool has already been released, do not drain it (double release).

I've resorted to setting a boolean at the end of the retrieval completed thread and then waiting for it to be true in the main thread, but I feel unclean :)

Any ideas?
« Last Edit: December 02, 2020, 10:22:09 pm by trev »
o Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
o Lazarus 2.1 r64392 3.3.1 Jan 13 21:24 macOS 11.1 aarch64 Xcode 12.3
o Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
o Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
o Lazarus 2.0.10 3.2.0 Win10 Parallels VM

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 818
Re: Thread synchronisation not going to plan...
« Reply #1 on: December 02, 2020, 12:54:49 pm »
I'm working on example code to retrieve a web page using NSURLSession custom delegates instead of the deprecated NSURLConnection.

In my custom delegate TMyDelegate.URLSession_task_didCompleteWithError(session: NSURLSession; task: NSURLSessionTask; error: NSError) procedure I inserted RTLEventSetEvent(ThreadDidFinish) and in the main thread I have RTLEventWaitFor(ThreadDidFinish) before attempting to display the retrieved data.
Do you also initialise the rtlevent first? It seems to be missing (even in the comments) in the wiki article.

Quote
I've resorted to setting a boolean at the end of the retrieval completed thread and then waiting for it to be true in the main thread, but I feel unclean :)
It's not only unclean, but also unreliable. You cannot safely synchronise between two threads by simply setting and reading variables. You always need memory barriers as well (which are implicit when using synchronisation primitives like RTLEvent).

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1222
  • Former Delphi 1-7, 10.2 User
Re: [SOLVED] Thread synchronisation not going to plan...
« Reply #2 on: December 02, 2020, 10:26:34 pm »
Thanks Jonas - I did indeed forget to initialise the rtlevent and couldn't see the wood for the trees. Now fixed.

I'll go reread the memory barrier text you referred me to earlier.
o Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
o Lazarus 2.1 r64392 3.3.1 Jan 13 21:24 macOS 11.1 aarch64 Xcode 12.3
o Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
o Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
o Lazarus 2.0.10 3.2.0 Win10 Parallels VM

 

TinyPortal © 2005-2018