Recent

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

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2020
  • 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 »

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
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: 2020
  • 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.

 

TinyPortal © 2005-2018