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

Author Topic: Thread gets put to sleep . How can i avoid that ?  (Read 5910 times)

0 Members and 2 Guests are viewing this topic.

Ivysaur

  • Newbie
  • *
  • Posts: 4
    • View Profile
Thread gets put to sleep . How can i avoid that ?
« on: February 12, 2015, 10:12:00 pm »
I was programming my Game until i found a Problem with Threads .
By the Way i got all my Functions and Variables in one Class (Besides the main) .

int main()
{
        Game Game1;   //Create an Instance of the Game and initialise it

        sf::Thread THGetKeyInput(&Game::GetKeyInput,&Game1);
        THGetKeyInput.launch();             //Create and launch the Thread which Function takes Events from the
                                                            //Main Window and puts them in bools in the Game1 Instance

        Game1.menu();   //Start the actuall Game . Here is the Problem
}

What apparently happens is that the call of Function Game1.menu puts the main Function to sleep .
And because THGetKeyInput is a part of the main Function it gets put to sleep too .

Is there any solution to this Problem while still just creating and running a Thread once ?

I've tried to make a Thread for both Functions and adding while() to the main Function but then
the Game1.menu Function seems to be unable to draw Sprites in the Window .

If the Problem is something else pls tell me .

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #1 on: February 12, 2015, 10:31:56 pm »
First question that comes to mind is; why on earth are you even bothering with threads?

If "Game1.menu()" puts the program (the whole program, not just the main thread) to sleep, you should already know why since you wrote it. Besides, you didn't even show us the code of the function, so how would we know what it does?

And I have to ask again - why threads?

Ivysaur

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #2 on: February 12, 2015, 10:51:30 pm »
First question that comes to mind is; why on earth are you even bothering with threads?

If "Game1.menu()" puts the program (the whole program, not just the main thread) to sleep, you should already know why since you wrote it. Besides, you didn't even show us the code of the function, so how would we know what it does?

And I have to ask again - why threads?
Why shouldn't i use threads ? Are they really that unstable or should i just use as less as possible ?

By the way Game1.menu just displays some Sprites and checks for the bools that are being modified by THGetKeyInput . The Problem here is that THGetKeyInput get's put to sleep because it's local in the main Function which waits until Game1.menu returns and therefor those bools will always be false .

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #3 on: February 12, 2015, 11:25:17 pm »
Threads are not unstable. They just add a level of complexity that is rarely needed. In most case, you can make a whole game with only one thread without a problem. I really don't see why you use thread for this.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #4 on: February 13, 2015, 07:43:54 am »
And if you still want to use threads you should use std::thread.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Ivysaur

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #5 on: February 13, 2015, 07:52:19 am »
Threads are not unstable. They just add a level of complexity that is rarely needed. In most case, you can make a whole game with only one thread without a problem. I really don't see why you use thread for this.
If you don't wanna help . Then just stfu .

Programming is not something that you need to do this or this way . Instead you can use many different Ways to achieve your Goal . And if you don't understand my Way of programming then don't question it out . If you think Threads are too complex Then fine . Just don't say that others shouldn't use them . Because it's not your decision .

Ivysaur

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #6 on: February 13, 2015, 07:54:22 am »
And if you still want to use threads you should use std::thread.
Maybe i'll get to that later . My Compiler just doesn't support it and changing to a different one will put me in the same Struggles i had when trying to install SDL on Dev-C++ .

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #7 on: February 13, 2015, 12:38:36 pm »
If you don't wanna help . Then just stfu .

Programming is not something that you need to do this or this way . Instead you can use many different Ways to achieve your Goal . And if you don't understand my Way of programming then don't question it out . If you think Threads are too complex Then fine . Just don't say that others shouldn't use them . Because it's not your decision .
Please calm down and stay friendly. This is a public forum and anyone can write their opinion. Even if you've opened this thread, it's not your "property" and as such you can't tell people what to do either.
Besides it's rather ignorant to not at least trying to listen to people who might have (more) experience with a certain topic. If you want to do X, that's fine, but if you can't figure X out on your own and ask for other people's help, you also have to be open for criticism and questions.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Thread gets put to sleep . How can i avoid that ?
« Reply #8 on: February 13, 2015, 05:13:39 pm »
It is a fact that threads add a lot of complexity.
Just to mention a few things:
They share the same address space so you have to be careful with synchronous (read/write) access to variables/objects and use proper locks/atomics (remember, just one data race in your program results in the entire thing having "undefined behaviour").
You have to deal with issues like deadlocks, livelocks and priority inversion.
You can sometimes gain performance but also often make performance worse due to bad thread scheduling and lock contention.
Your program becomes harder to reason about.
They are often not needed at all.
And much, much more.

As for your specific problem; you haven't really shown enough code for people to give some good advice and help you.
You should consider creating/posting a Short, Self Contained, Correct (Compilable), Example and also read a bit on How To Ask Questions The Smart Way.
« Last Edit: February 13, 2015, 07:47:50 pm by Jesper Juhl »