Do you have an advice about how to replace FreeOnTerminate on threads ?

Do you have an advice about how to replace FreeOnTerminate on threads ? 
I have an application that uses several threads (~15 ) , and sometimes I have this error message : "Cannot terminate externally created thread” that seems to pop up from nowhere, The point is that I don't use externally created thread, I only use mine.
I suspect this problem comes from FreeOnTerminate = True on some threads.

How do you safely replace this  ? An event raised at the end of the Execute method ? A call to a main thread method (may be inside a Synchronize) ? A window message ?

Comments

  1. you can assign Thread.OnTerminate which has the TNotifyEvent signature, on this event, you can free the thread.

    ReplyDelete
  2. P.S. never had an issue with "FreeOnTerminate := True", so I'd check a bit deeper.

    ReplyDelete
  3. Andrea Raimondi Is existing threads code migration to this library complicated ?

    ReplyDelete
  4. Do you use Indy?  Indy creates it's own threads too.  I remember having some issues with killing threads that had Indy http sessions going.

    Do you use EurekaLog? It should be able to help you find the origin of the message.

    Otherwise, I seem to only have  FreeOnTerminate := False statements.

    ReplyDelete
  5. Lars Fosdal I use HotLog, which uses threads AFAIK. But if I use HotLog.pas , I think that threads created are not considered as externally created .... But .... I'm not quite sure of that.

    ReplyDelete
  6. Olivier SCHWAB It's a different programming model( tasks vs threads ) but it's comprehensive and if you have many threads certainly worth looking at.

    I don't use much threaded code for two reasons:
    1) I don't need it
    2) On today's hardware, many threaded solutions may end up being slower on multiple cores.

    However the library is well respected and with a fairly large following and worth a look :)

    A

    ReplyDelete
  7. Andrea Raimondi OK I'll study that lib anyway.
    BTW someone once said here something that made me laugh alot :

    " I had 1 problem. I resolved it by using threads. Now I have 3 problems" ^^

    ReplyDelete
  8. Andrea Raimondi I'm not really sure OTL would help here. It uses TThread internally so Olivier may still run into the same problem. OTOH, it is true that all TThreads are terminated explicitly in the OTL and that it is easy to convert - just use CreateTask(task_method).Unobserved.Run where task_method is your old TThread.Execute (although you have to move it into its own method or class). There should not be any need for big modifications of the existing code.

    ReplyDelete
  9. Olivier SCHWAB If you have any question regarding OTL, just ask.

    ReplyDelete
  10. Thank you very much ! I'll try to have a look to your lib next week.
    I might a small project that will be perfect to start with OTL

    ReplyDelete
  11. Olivier SCHWAB I haven't tried OTL yet, but I have had some good experience with AsyncCalls by Andreas Hausladen <http://andy.jgknet.de/blog/bugfix-units/asynccalls-29-asynchronous-function-calls/>

    ReplyDelete
  12. AsyncCalls is also an excellent library, although the development has stopped. Too bad :(

    ReplyDelete
  13. Anyway thanks to all for your interest

    ReplyDelete
  14. Olivier SCHWAB do a Google search on the error message, it returns results.

    ReplyDelete
  15. Already googled on it. The point is that, AFAIK i don t use external threads, and thé result google ogives mainly concern api functions ...
    as an example, this  http://embarcadero.newsgroups.archived.at/public.delphi.nativeapi/200902/090224921.html   doesn't give me any clue for my problem...

    ReplyDelete

Post a Comment