vendredi 26 juin 2015

Threads handling in C++ 11

I have a base class, that manages threaded functions and derived, which can set it's functions run concurrently with help of that base class. But during the test in which I emulating fast starts/stops of those function, my programs crashes. Seems, the problem is in mutexes but I can be wrong. What is the problem?

class ThreadedBase
{
public:
    ThreadedBase(){}
    virtual ~ThreadedBase() {
        for (int i = 0; i < m_threadedTasks.size(); ++i) {
            *m_threadedTasks[i]->run = false;
            m_threadedTasks[i]->thread->join();
        }

        while (m_threadedTasks.size()) {
            m_threadedTasks.erase(m_threadedTasks.begin());
        }
    }

    void addTask(std::function<void()> f) {
        static int i = 0;
        m_threadedTasks.insert({ i++, new ThreadedTask(f)});
    }

    void startN(int n) {
        std::lock_guard<std::mutex> lock(m_threadedTasks[n]->mtx);
        std::cout << "StartN\n";
        if (*m_threadedTasks[n]->run == true) return;
        *m_threadedTasks[n]->run = true;
         m_threadedTasks[n]->thread = new std::thread((std::mem_fn(&ThreadedBase::run)), this, n);
    }

    void stopN(int n) {
        std::lock_guard<std::mutex> lock(m_threadedTasks[n]->mtx);
        std::cout << "StopN\n";
        *m_threadedTasks[n]->run = false;
    }

private:
    struct ThreadedTask
    {
        ThreadedTask(std::function<void()> f) : function(f), thread(nullptr), run(new bool(false)) {}
        ~ThreadedTask() {
            thread->join();
            delete thread;
            delete run;
        }
        std::function<void()> function;
        std::thread *thread;
        mutable std::mutex mtx;
        bool *run;
    };
    std::unordered_map<int, ThreadedTask*> m_threadedTasks;

    void run(int n) {
        while (*m_threadedTasks[n]->run) {
            std::lock_guard<std::mutex> lock(m_threadedTasks[n]->mtx);
            m_threadedTasks[n]->function();
            std::cout << "yet another loop\n";
        } 
        // todo: erase if needed
        std::lock_guard<std::mutex> lock(m_threadedTasks[n]->mtx);
        *m_threadedTasks[n]->run = false;
        delete m_threadedTasks[n]->thread;
        m_threadedTasks[n]->thread = nullptr;
    }
};

class Test : public ThreadedBase
{
public:
    Test() : ThreadedBase() {
        addTask(std::bind(std::mem_fn(&Test::threadedBlock1), this));
        addTask(std::bind(std::mem_fn(&Test::threadedBlock2), this));
    };
    void threadedBlock1() {
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        std::cout << "Task1 finished ";
    }
    void threadedBlock2() {
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        std::cout << "Task2 finished ";
    }
    ~Test() { std::cout << "Deleting\n"; }

    void startN(int n) {
        ThreadedBase::startN(n);
    }
    void stopN(int n) {
        ThreadedBase::stopN(n);
    }
};

int main()
{
    Test *t = new Test();

    srand(time(NULL));
    while (true) {
        (0 + rand() % 2 == 0) ? t->startN(0) : t->stopN(0);
    }

    std::this_thread::sleep_for(std::chrono::seconds(100));

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire