any good design for thread function

Hi,
I'm trying to write a common thread class, and found it hard to design well for killing the thread instantly.

1
2
3
4
5
6
7
void thread_func() {
   for(int try_=0; try_<10; try_++) {
      if(do_some_job()) {
         return;
      }
   }
}

the problem is: if I run the function with a thread, and I want the thread can be killed instantly, I need to do something like:
1
2
3
4
5
6
7
8
bool killed = false;
void thread_func() {
	for(int try_=0; try_<10 && !killed; try_++) {
		if(do_some_job()) {
			return;
		}
	}
}

and add a killed flag in do_some_job(), so it can return instantly when killed,
this looks ugly, if do_some_job() contains do_some_more_job()...

anyone has experience with this scenario?

Thanks.
In my experience, that has nothing wrong about it. But you must know for sure what you are doing. In multi-threading environments you MUST synchronize variable access. If you are unsure about your synchronization skills, then exchange the bool control variable with a synchronization object.
Thanks, my case is quite simple, not necessary for synchronization yet, but now I think it's actually not about the thread, it's about design something like
class stoppable {}
or
class timeout_task{}
Topic archived. No new replies allowed.