none
How to create and run multi- tasks via task_group? RRS feed

  • 问题

  • i want to create and run multi tasks via task_group in a function, the  code is :

    void ProcessRequest()
     {
      //auto workerthread;
       for(int i = 0; i < m_nThreadNum; ++i )
       {
        task_group tg;
        auto workerthread = make_task(
       [&] {
        while(true)
        {
         Request request = getRequest();
         cout<<"Worker Thread:"<<i<<"正在执行 ";
         request.execute();
        }
       });
        tg.run(workerthread); // missing_wait exception
       }
      
     }

    but, when i execute this function, i got a missing_wait exception. my question is how to create and run multi tasks, for example 20, via task_group? Or is there any other way? 

    2011年1月11日 3:07

答案

  • Hi Liangqiao,

    >>when i execute this function, i got a missing_wait exception.

    Missing_wait 类,此类描述每当执行对象的析构函数时仍然将任务计划至 task_group structured_task_group 对象时引发的异常。 如果析构函数因为堆栈展开为异常的结果而被达到,将永远不会引发此异常。

    运行时引发该异常表明您忘记了调用 wait run_and_wait 方法。请您在允许析构 task_group structured_task_group 对象之前,调用对象的 wait run_and_wait 方法。

     

    如果您的问题解决了,请把有用的回答标记为答案!

    谢谢,

    Lucy

     


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月12日 8:38
    版主

全部回复

  • i want to create and run multi tasks via task_group in a function, the  code is :

    void ProcessRequest()
     {
      //auto workerthread;
       for(int i = 0; i < m_nThreadNum; ++i )
       {
        task_group tg;
        auto workerthread = make_task(
       [&] {
        while(true)
        {
         Request request = getRequest();
         cout<<"Worker Thread:"<<i<<"正在执行 ";
         request.execute();
        }
       });
        tg.run(workerthread); // missing_wait exception
       }
      
     }

    but, when i execute this function, i got a missing_wait exception. my question is how to create and run multi tasks, for example 20, via task_group? Or is there any other way? 


    the whole cpp file is :

    // WorkerThread.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <queue>
    #include<iostream>
    #include <ppl.h>
    #include <time.h>

    using namespace Concurrency;
    using namespace std;
    class Request
    {
    public:
     Request(unsigned int nID)
      : m_nID(nID)
     {}
     void execute()
     {
      cout<<"请求:"<<m_nID<<endl;
     }
     unsigned int getID()
     {
      return m_nID;
     }
    private:
     unsigned int m_nID;
    };

    // 请求队列
    // 客户端将请求添加进入这个队列
    // 服务器从这个队列中获取来自客户端的请求进行处理
    class RequestQueue
    {
    public:
     RequestQueue( unsigned int nThreadNum)
     {
      m_nThreadNum = nThreadNum;
     }
     void ProcessRequest()
     {
      //auto workerthread;
       for(int i = 0; i < m_nThreadNum; ++i )
       {
        auto workerthread = make_task(
       [&] {
        while(true)
        {
         Request request = getRequest();
         //cout<<"Worker Thread:"<<i<<"正在执行 ";
         request.execute();
        }
       });
        tg.run(workerthread);
       }
      
     }
     // 获取请求,如果当前请求队列为空,则进行等待
     Request getRequest()
     {
      bool bWait = true;
      while( bWait )
      {
       wait(500);
       cs.lock();
       bWait = m_Queue.size() > 0 ? false : true;
       cs.unlock();
      }
      cs.lock();
      Request req = m_Queue.front();
      m_Queue.pop();
      wait(1000);
      cout<<"GET:"<<req.getID()<<endl;
      cs.unlock();
      return req;
     }
     // 将请求加入请求队列中
     void setRequest(Request req)
     {
      bool bWait = false;
      do
      {
       cs.lock();
       bWait = m_Queue.size() < 5 ? false : true;
       if(bWait)
       {
        cout<<"Wait..."<<endl;
        wait(500);
       }
       cs.unlock();
      }while(bWait);
      cs.lock();
      m_Queue.push(req);
      cout<<"SET:"<<req.getID()<<endl;
      cs.unlock();
     }
    private:
     critical_section cs; // 为了保护队列访问的临界区
     queue<Request> m_Queue;
     unsigned int m_nThreadNum;
     task_group tg;
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
     srand((int)time(NULL));

     RequestQueue queue(10);
     queue.ProcessRequest();
     // 模拟多个客户端的请求
     auto request = make_task( [&]() {
      int nCount = 0;
      while(nCount<20)
      {
       wait(rand()%10);
       queue.setRequest(Request(nCount));
       ++nCount;
      }
     });
     // 模拟客户端的处理
     auto process = make_task( [&]() {
      while(true)
       queue.getRequest();
     });

     // 执行请求和处理的线程
     task_group tg;
     tg.run(process);
     tg.run_and_wait(request);

     return 0;
    }

     

     

     

    2011年1月11日 3:10
  • Hi Liangqiao,

    >>when i execute this function, i got a missing_wait exception.

    Missing_wait 类,此类描述每当执行对象的析构函数时仍然将任务计划至 task_group structured_task_group 对象时引发的异常。 如果析构函数因为堆栈展开为异常的结果而被达到,将永远不会引发此异常。

    运行时引发该异常表明您忘记了调用 wait run_and_wait 方法。请您在允许析构 task_group structured_task_group 对象之前,调用对象的 wait run_and_wait 方法。

     

    如果您的问题解决了,请把有用的回答标记为答案!

    谢谢,

    Lucy

     


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月12日 8:38
    版主
  • Hi liangqiao,

        我先暂时把我的回答标记为答案,如果您认为这回答没有帮助到您,您可以取消标记为答案。

     

    谢谢,

    Lucy



    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年1月20日 10:00
    版主