1 /***************************************************************************
2 * Copyright (C) 2009 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "WaitableEvent.h"
26 * Repeatedly executes a function until \ref Terminate
27 * is called. The function should not be long-running.
28 * If the function returns \a true, the thread will terminate
29 * automatically and delete the CThread instance.
31 * \ref Suspend and \ref Resume will not wait until the
32 * thread actually enters the respective state.
33 * \ref Terminate, however, will only return when \ref func
34 * has exited and will not be called again.
36 * Please note that this class is designed to be controlled
37 * by _one_ outer thread. Trying to control it from multiple
38 * threads may result in unstable behavior.
52 /// if set, the thread function should exit asap
54 volatile bool terminated
;
56 /// set, if the thread function has terminated
60 /// allow for the thread to become inactive
62 volatile bool suspended
;
63 CWaitableEvent suspend
;
64 CWaitableEvent resume
;
66 /// to be executed cyclicly
71 /// the actual thread function
73 static void ThreadFunc (void*);
77 /// auto-start thread during construction
79 CThread (bool (*func
)(void *), void* args
, bool startSuspended
= false);