Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Find out when a thread is done or is running?  (Read 4070 times)

0 Members and 1 Guest are viewing this topic.

kreed

  • Newbie
  • *
  • Posts: 14
    • View Profile
Find out when a thread is done or is running?
« on: September 08, 2012, 06:04:24 pm »
So, I need to find out when a thread is done. Is this possible? I need to use it so that when a specific thread is closed, my other thread terminates. Also, is there a way to find out if a thread is currently running? Essentially, I have two threads. When any of my threads stop running, I want to terminate all my other threads.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Find out when a thread is done or is running?
« Reply #1 on: September 08, 2012, 06:12:22 pm »
SFML doesn't provide functionalities to check this, but it can be more or less easily get implemented.
For example you have a boolean variable in a shared scope and when the thread starts you lock it, then set it to true, unlock it, do your thing and at the end lock, unset and unlock it.
Mean while you'll be able to check if the boolean is set or not and thus get the current 'state' (you'll also need to lock it from the other threads, against what many developers might say, booleans are not guaranteed to be atomic). ;)

There are also other ways and maybe with the right design you won't even need them... ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kreed

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Find out when a thread is done or is running?
« Reply #2 on: September 08, 2012, 06:18:36 pm »
Yeah, I thought about doing something like that. This is really just a placeholder so I can get something working quickly. Later I'll terminate from inside the thread using my event system. I just wanted a quick fix to move forward :P.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Find out when a thread is done or is running?
« Reply #3 on: September 08, 2012, 06:49:54 pm »
Later I'll terminate from inside the thread using my event system.
Although I guess you didn't mean it that way, I still wanted to point out that calling terminate() on a thread isn't a good solution, it's better just to let the thread naturally finish (i.e. it gets to the end of the threaded function). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything