FGx
1
src
utilities
workThread.h
1
/* ==================================================
2
Threading project
3
Created: Geoff R. McLane - Aug 2011
4
License: GPL2 (or later)
5
copied to FGx project
6
================================================== */
7
/* ********************************************************
8
* workThread.cpp[.h]
9
*
10
* Created by Geoff R. Mclane, Paris
11
* (c) Aug 2011 GPL2 (or later)
12
*
13
* Create a simple 'worker' thread, using QThread
14
* Instantiation: Either through declaration
15
* workThread thread, or allocated
16
* workThread * worker = new workThread, and remember
17
* to do 'delete worker' at end of the application.
18
*
19
* API: Call job_number = worker->work( function ), with the function
20
* to be run. Function must be void foo() type.
21
* 2011-08-29: Or the function can be void foo(void *) type
22
* This can be called multiple times, and the 'jobs' will be stacked,
23
* and processed one after the other.
24
*
25
* SIGNAL: workThread emits a work_done(int,int) when completed, passing the
26
* job number, and ms of running, so use
27
* connect(worker, SIGNAL(work_done(int,int)),
28
* this, SLOT(done_it(int,int)));
29
* to connect this to a SLOT job_done(int job_number, int ms)
30
*
31
* Presently, if processing a LONG job at application exit, the full exit of
32
* the application will only happen when the job is done.
33
* A future enhancement would be to provide a 'cancel' mechanism, to enable
34
* killing the thread even if 'working'...
35
*
36
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37
** "AS IS" WITH NO EXPRESS OR IMPLIED WARRANTIES OF ANY TYPE.
38
*
39
******************************************************** */
40
#ifndef WORKTHREAD_H
41
#define WORKTHREAD_H
42
/* =======================================================
43
HISTORY: Growing upwards
44
2011/12/14 - In qt_osm_map project, remove macro - always use (void *), and
45
removed 'WorkType' enum. change to int, defined by user.
46
Add loginfo(QString), for easy change of outLog(msg)
47
2011/08/31 - Add passing of a void * to the function, under macro ADD_VOID_PTR
48
2011/08/24 - First implementation in qt_test_gui/src/workThread.cpp
49
======================================================= */
50
#include <QThread>
51
#include <QMutex>
52
#include <QWaitCondition>
53
#include <QTime>
54
#include <QList>
55
#include "utilities.h"
// for outLog(msg) and getElapTimeStg(ms)
56
57
typedef
void (*ACTION)(
void
*);
58
typedef
struct
tagWORKITEM
{
59
ACTION act;
60
void
* vp;
61
int
user_type;
// defined by user
62
bool
abort;
63
}
WORKITEM
, *
PWORKITEM
;
64
typedef
QList<WORKITEM> THREAD_LIST;
65
66
class
workThread
:
public
QThread
67
{
68
Q_OBJECT
69
public
:
70
workThread
(QObject *parent = 0);
71
~
workThread
();
72
bool
in_function;
73
bool
was_terminated;
74
int
work(ACTION func,
void
*vp);
75
void
loginfo(QString msg) {
76
#ifdef GEN_UTILITIES_H
77
outLog(msg);
78
#else
79
Q_UNUSED(msg);
80
#endif // #ifdef GEN_UTILITIES_H
81
82
}
83
84
signals:
85
void
work_done(
int
jn,
int
ms);
86
87
protected
:
88
void
run();
89
90
private
:
91
QMutex mutex;
92
QWaitCondition condition;
93
int
restart;
94
bool
abort;
95
THREAD_LIST jobs;
96
int
jobs_in;
97
int
jobs_out;
98
int
cyc_count;
99
int
cum_running;
100
QTime wt;
101
};
102
103
// a convenient SLEEP (ms) function
104
class
SleeperThread
:
public
QThread
105
{
106
public
:
107
static
void
msleep(
unsigned
long
msecs)
108
{
109
QThread::msleep(msecs);
110
}
111
};
112
113
// structure for the application
114
// to keep track of 'work' passed to thread
115
typedef
struct
tagWORK
{
116
int
work_type;
// will determine actions when DONE
117
int
work_num;
118
QTime work_tt;
119
QString work_desc;
120
bool
work_done;
121
bool
abort_work;
122
}
WORK
, *
PWORK
;
123
124
typedef
QList<PWORK> WORK_LIST;
125
126
#endif // WORKTHREAD_H
127
// eof - workthread.h
SleeperThread
Definition:
workThread.h:104
workThread
Definition:
workThread.h:66
tagWORK
Definition:
workThread.h:115
tagWORKITEM
Definition:
workThread.h:58
Generated on Thu Feb 13 2020 01:10:59 for FGx by
1.8.17