File: TaskManager.h

package info (click to toggle)
openterface-qt 0.1.0%2Bds-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 1,444 kB
  • sloc: cpp: 9,552; sh: 127; python: 57; ansic: 4; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 774 bytes parent folder | download
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
#ifndef TASKMANAGER_H
#define TASKMANAGER_H

#pragma once

#include <QObject>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
#include <functional>

class TaskManager : public QObject
{
    Q_OBJECT
public:
    static TaskManager* instance();
    void addTask(std::function<void()> task);

private:
    TaskManager();
    ~TaskManager();

    class Worker;
    Worker *m_worker;
    QThread m_workerThread;
};

class TaskManager::Worker : public QObject
{
    Q_OBJECT
    friend class TaskManager;

public:
    Worker();
    ~Worker();

private slots:
    void onProcessTasks();

private:
    QQueue<std::function<void()>> m_taskQueue;
    QMutex m_mutex;
    QWaitCondition m_condition;
    bool m_exit = false;
};

#endif // TASKMANAGER_H