Hi
I'm working on a text based game engine, because I have to run number of things at the same time I use threads. From my little understanding of threads I know that the thread ends when the function returns. My problem with this is that in my game I have to use about 10 threads for each level... :\ (they are all in loops). So my question is: Is there a way to end a thread manually?
> I know that the thread ends when the function stop.
So, write the thread function such that it finishes (exits from the loop) when a flag is set.
For example:
#include <iostream>
#include <thread>
#include <atomic>
#include <chrono>
void thread_fun( std::atomic<bool>& quit_flag, char id, unsignedint interval_millisecs )
{
constauto sleep_time = std::chrono::milliseconds(interval_millisecs) ;
while( !quit_flag ) // keep running as long as quit flag is not set
{
std::cout << id << std::flush ;
std::this_thread::sleep_for(sleep_time) ;
}
std::cout << id << " : quitting\n" << std::flush ;
}
int main()
{
constchar cstr[] = "abcdefghijklmnop" ;
std::atomic_bool quit_flag(false) ;
std::cout << "press enter to end program\n" ;
// start ten threads (note: quit flag is passed by wrapped reference)
for( int i = 0 ; i < 10 ; ++i )
std::thread( thread_fun, std::ref(quit_flag), cstr[i], (i+1)*100 ).detach() ;
std::cin.get() ;
quit_flag = true ; // pogram ending; set the quit flag
// and (ideally) wait for the threads to exit gracefully
std::this_thread::sleep_for( std::chrono::milliseconds(1500) ) ;
}
That's not a design error, it's just the way things are.for example, I have a class of rectangles, each rectangle has a move() function. If I want 2 rectangles to move at the same time (smoothly, in a loop that moves 1 at a time) I have to use threads. Now think about a level that have something like 10 moving rectangles -> 10 threads.
I'm aware of this problem and if you have an alternative method I'll be more than happy to hear about it.
What do you mean by 'manually'?
I just want to have a function that takes a thread as a parameter and terminates/stops the given thread. Something like void Stop_Thread(thread th)
thread functions are like any other.
if condition 'return' to exit
now, your question may be 'how do I get the condition to the thread' ... that can be more complicated. If it can't detect it, it has to be passed in somehow; maybe a pointer to a bool or something...
there may also be ways to spawn the threads such that you can kill them externally. I have never had to do that, but it seems like something the tools should have somewhere.
30 threads is possibly too many on a standard computer. You may want to look at some sort of dispatch type design or other way to reduce.
> If I want 2 rectangles to move at the same time (smoothly, in a loop that moves 1 at a time) I have to use threads.
You're going to be spending most of your time in thread context switches if you have one thread for each graphic primitive draw function.
This whole "at the same time" is an illusion.
Your screen refresh rate is say 60Hz. This gives you about 17 milliseconds between each frame.
Now you can draw your scene as a simple loop
for ( auto && r : array ) r.draw();
Or you can mess with threads.
Either way, so long as both complete with 17 milliseconds, the illusion of "at the same time" is maintained.
You might have an update thread and a render thread.
But you wouldn't want a thread for every object.
First thing first thanks for the responds. It turned out I was stupid and didn't explain myself right. I don't want 30 threads at the same time. It's just that every level has about 10 object that have some sort of functionality. I total that means that I have to have about 40 functions in my code just for the objects (not including the functions that manages that chaos). This is an unimaginably inefficient code and clearly not an option.
Now you can draw your scene as a simple loop
Unfortunately, it's not that simple, the engine is pretty advanced by now and it's not just movable rectangles. The engine now have interactive object and stuff like that, they need a functionality of their own in order to do specific stuff (for example: open a door).