Update rebase documentation
[TortoiseGit.git] / src / AsyncFramework / Thread.cpp
blobf986071674a1497786318f9c0fceb455c30cc19f
1 /***************************************************************************
2 * Copyright (C) 2009 by Stefan Fuhrmann *
3 * stefanfuhrmann@alice-dsl.de *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #include "stdafx.h"
22 #include "Thread.h"
24 namespace async
27 /// the actual thread function
29 void CThread::ThreadFunc (void* arg)
31 // CCrashReportThread crashthread;
32 CThread* self = reinterpret_cast<CThread*>(arg);
34 // maybe, we were started in "suspended" mode
36 if (self->suspended)
37 self->resume.WaitFor();
39 // main loop
41 while (!self->terminated)
43 if ((*self->func)(self->args))
45 // auto-deletion
47 self->terminated = true;
48 self->done.Set();
49 delete self;
51 return;
54 if (self->suspended)
55 self->resume.WaitFor();
58 // indicate that the thread is no longer processing any data
60 self->done.Set();
63 /// auto-start thread during construction
65 CThread::CThread (bool (*func)(void *), void* args, bool startSuspended)
66 : thread (NULL)
67 , terminated (false)
68 , suspended (startSuspended)
69 , func (func)
70 , args (args)
72 thread = _beginthread (&ThreadFunc, 0, this);
75 CThread::~CThread(void)
77 if (thread != 0)
78 Terminate();
81 /// thread control
83 void CThread::Suspend()
85 resume.Reset();
86 suspended = true;
89 void CThread::Resume()
91 suspended = false;
92 resume.Set();
95 void CThread::Terminate()
97 terminated = true;
99 Resume();
100 done.WaitFor();