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

Author Topic: Can't terminate thread  (Read 4457 times)

0 Members and 2 Guests are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Can't terminate thread
« on: June 12, 2014, 08:19:24 am »
I was playing with this, trying to get the feel for it.
This is my first time using this library as-well as using threads. (Go ahead, criticise my code :D)
And I can't terminate the thread. When I press space, my program just freezes and when I add a breakpoint it says



Heres the code:
Code: [Select]
#include <SFML/System.hpp>
#include <string>
#include <iostream>
#include <Windows.h>
void spam(std::string phrase);
bool gotkey = false;
int main()
{
std::string spamwhat;
std::cout << "What should I spam?: ";
std::getline(std::cin, spamwhat);
sf::Thread spammer(&spam, spamwhat);
spammer.launch();
while (true)
{
if (GetAsyncKeyState(VK_SPACE))
{
spammer.terminate();
std::cout << "What should I spam now?: ";
std::getline(std::cin, spamwhat);
sf::Thread spammer(&spam, spamwhat);
spammer.launch();
}
Sleep(100);
}
}

void spam(std::string phrase)
{
while (true)
{
std::cout << phrase << std::endl;
}
}

Could you please help me?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't terminate thread
« Reply #1 on: June 12, 2014, 08:52:51 am »
If you post code you should use the [code=cpp]// Your code[/code] tag. ;)

The problem is that you're creating a new thread object inside the if-block and since the thread object waits for the thread to finish executing at destruction and leaving the if-block destroys the newly defined thread object, the if-block is never finished. Besides that you might get some naming conflict since you use "spammer" for two different variables.

Additionally your code will never exit since your outer loop has no way to stop.

And calling terminate() is really bad. It's there for emergencies, but otherwise you should always let the thread finish on its own. Terminate will just "kill" the thread and leaving variables and other stuff in a uncertain state.

As for the code, you could simply use sf::Keyboard::isKeyPressed(sf::Keyboard::Space) instead of the OS specific code.
Also if you actually intend to do some thread development, you should get yourself a book on that topic, because it's not trivial at all. There's a lot that one has to learn and that won't come natural to you. Things like: What are race conditions? When is something thread-safe? How do you synchronize shared data? What are lock-free containers? etc. etc. If you don't have a specific reason to do parallel programming, it's usually not worth investigating too much.
As a last suggestion, use std::thread if your compiler supports C++11. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can't terminate thread
« Reply #2 on: June 12, 2014, 09:33:19 am »
Terminating threads is actually so bad that the Java Thread.stop() method has been deprectated, and the C++ std::thread class doesn't even include such a method. Do not terminate threads.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Can't terminate thread
« Reply #3 on: June 12, 2014, 09:57:13 pm »
Alright, thank you all for your help!