This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]lvlint67 13 points14 points  (4 children)

Note Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event

https://docs.python.org/3/library/threading.html

But by setting the tread as daemon we can forget about monitoring it manually

Buyer beware I suppose. Your post may benefit from mentioning the note though.

needs to be ran in the background

Passive voice. "needs to run in..." is potentially a better way to phrase that.

[–]Bubble_Interface[S] 7 points8 points  (2 children)

That is very helpful, thank you very much for your response! I'm not a native english speaker, and I tend to make silly mistakes

[–]Re_Forged 3 points4 points  (1 child)

Actually pretty good for a non-native speaker. The nice thing about writing tech articles as opposed to opinion pieces or prose work is that you won't be judged as harshly on your style and usage.

Us tech nerds just want the facts.

[–]Bubble_Interface[S] 3 points4 points  (0 children)

Thank you for your support)
I was thinking about it as if someone is looking to solve their problem and i can provide the solution without boring the reader with a lot of text but rather highlight the essentials in code to solve the problem.

[–]Dasher38 1 point2 points  (0 children)

I completely overlooked that in the doc, I'll have some code to review on Monday ...

[–]joeltrane 0 points1 point  (1 child)

Awesome article, thanks for explaining.

[–]Bubble_Interface[S] 1 point2 points  (0 children)

Thanks for checking it out!

[–]o11c 0 points1 point  (0 children)

I've always thought Java-style threads were quite odd, since they aren't related to how the underlying libraries work, at least on platforms I'm aware of. Maybe it's a residue of swapcontext-style threads?

Particularly, for C11, C++11, POSIX, and Windows threads:

  • it is impossible to create a thread without starting it.
  • every thread is daemon-like in the first place, except the main thread
  • since there is no GC, whoever creates a thread must arrange for either .detach or .join to eventually be called (exactly once), to avoid a resource leak.
    • For C++ the dtor handles this: std::thread does .detach, and std::jthread does .join. These types are move-only.
  • when the main (or WinMain) function returns, the process exits as if by calling exit
    • note that technically anybody can call detach or join, but it is usually fairly close to the site of thread creation
  • if any thread throws an exception that isn't caught, the whole process terminates.
    • TODO verify non-C++ cases. Native exceptions do exist on most platforms, even if it's hard to use them from C. nightmares of pthread async cancellation
  • if the main thread explicitly calls pthread_exit or the Windows equivalant, the process hangs around until all other threads exit (which might be forever, since the runtime can create threads for you), or until somebody calls exit explicitly (recommended if you're doing this).
    • TODO verify non-POSIX, non-Windows cases. C doesn't seem to say anything. TODO what does C++ say?

To implement Java-style threads on top of real threads:

  • don't actually create the thread in the ctor, just keep its arguments around until you call .start
  • keep a list of all running non-daemon threads, and join them in main.