检测线程完成以清理内存

时间:2016-12-06 00:02:27

标签: c++ multithreading asynchronous

我正在为某些事件制作异步任务管理,并制作了您可以在下面看到的代码。

现在,我正在处理向量futures中的异步任务;我的问题是,我不知道如何使用vector.erase()删除已经在向量中推送的任务,以避免向量future增长太多。

如何在删除之前知道异步线程何时完成?

我应该使用其他任何解决方案吗?

#include <iostream>
#include <future>
#include <vector>

using namespace std;

class asyncWrapper
{
public:
    asyncWrapper(std::string taskName, int creationTime, int itv_duration, int itv_start)
    {
        this->taskName = taskName;
        this->creationTime = creationTime;
        this->itv_duration = itv_duration;
        this->itv_start = itv_start;
        status = 0;
    };

    ~asyncWrapper() {};

    void printStatus(int a)
    {
        std::cout << "printClass" << a <<std::endl;
    }

    int called_from_async()
    {
        cout << "creationTime : " << creationTime << endl;
        cout << "interval : " << itv_duration << endl;
        cout << "startTime : " << itv_start << endl;

        cout << "status : 1 Event Recevied" << endl;

        bool bExit = false;
        bool bStart = false;

        while(!bExit && !IsCancle)
        {

            if(creationTime > itv_start && !bStart)
            {
                cout << "status : 2 Event Started" << endl;
                status = 2;
                bStart = true;
            }

            creationTime++;

            if(bStart)
            {
                itv_duration--;
                if(itv_duration < 0 )
                {
                    status = 3;
                    bExit = true;
                    cout << "status : 3 Event Completed : " << taskName.c_str() << endl;
                }
            }

            std::this_thread::sleep_for(std::chrono::seconds(1));
        }


        return 1;
    }

    void setCancle()
    {
        status = 6;
        IsCancle = true;

        cout << "status : 6 Event Canceled Task : " << taskName.c_str() << endl;
    }

private:
    std::string taskName;
    int creationTime;
    int itv_duration;
    int itv_start;
    int status;

    bool IsCancle = false;

    std::future<int> task;
};

int main() {

    std::vector<std::future<int>> futures; // place for asyns tasks

    asyncWrapper asyncTask1("Task 1", 1234560, 20, 1234570);
    asyncWrapper asyncTask2("Task 2", 1234560, 15, 1234570);
    asyncWrapper asyncTask3("Task 3", 1234560, 10, 1234570);
    asyncWrapper asyncTask4("Task 4", 1234560, 5, 1234570);
    asyncWrapper asyncTask5("Task 5", 1234560, 300, 1234570);

    futures.push_back(std::move(std::async(std::launch::async, &asyncWrapper::called_from_async, &asyncTask1)));
    futures.push_back(std::move(std::async(std::launch::async, &asyncWrapper::called_from_async, &asyncTask2)));
    futures.push_back(std::move(std::async(std::launch::async, &asyncWrapper::called_from_async, &asyncTask3)));
    futures.push_back(std::move(std::async(std::launch::async, &asyncWrapper::called_from_async, &asyncTask4)));
    futures.push_back(std::move(std::async(std::launch::async, &asyncWrapper::called_from_async, &asyncTask5)));
    cout << "asdlkasjasldjhasl das" << endl;

    asyncTask5.setCancle();

    for( auto & e : futures)
    {
        cout << e.get() << endl;
    }

    return 0;
}

0 个答案:

没有答案