summaryrefslogtreecommitdiff
path: root/library/multithreading.h
blob: b8481a0fd5ecaca88bdff631737d9e5fd14be505 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef MULTITHREADING_H_INCLUDED
#define MULTITHREADING_H_INCLUDED

#include <wx/string.h>
#include <wx/thread.h>

//interface for status updates (can be implemented by UI or commandline)
//overwrite virtual methods for respective functionality
class StatusUpdater
{
public:
    StatusUpdater() :
            abortionRequested(false) {}
    virtual ~StatusUpdater() {}

    //these four methods have to be implemented in the derived classes to handle error and status information
    virtual void updateStatusText(const wxString& text) = 0;
    virtual void initNewProcess(int objectsTotal, double dataTotal, int processID) = 0; //informs about the total amount of data that will be processed from now on
    virtual void updateProcessedData(int objectsProcessed, double dataProcessed) = 0;   //called periodically after data was processed
    virtual int  reportError(const wxString& text) = 0;

    //this method is triggered repeatedly and can be used to refresh the ui by dispatching pending events
    virtual void triggerUI_Refresh() = 0;

    void requestAbortion() //opportunity to abort must be implemented in a frequently executed method like triggerUI_Refresh()
    {                      //currently used by the UI status information screen, when button "Abort is pressed"
        abortionRequested = true;
    }

    static const int continueNext = -1;
    static const int retry        = -2;

protected:
    bool abortionRequested;
};


const int uiUpdateInterval = 100; //perform ui updates not more often than necessary, 100 seems to be a good value with only a minimal performance loss

class WorkerThread;


//class handling execution of a method while updating the UI
class UpdateWhileExecuting
{
    friend class WorkerThread;

public:
    UpdateWhileExecuting();
    virtual ~UpdateWhileExecuting();

    void waitUntilReady();
    void execute(StatusUpdater* statusUpdater);


private:
    //implement a longrunning method without dependencies (e.g. copy file function); share input/output parameters as instance variables
    virtual void longRunner() = 0;

    WorkerThread* theWorkerThread;

    wxMutex     readyToReceiveResult;
    wxCondition receivingResult;
};

#endif // MULTITHREADING_H_INCLUDED
bgstack15