thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "xiedongkai" <xiedong...@aliyun.com>
Subject Is Thrift aysc mode thread safe
Date Mon, 07 Nov 2016 11:48:21 GMT
hi:    i am using Thrift with async mode.  The processor handler will transfer callback
 to  pool.  trhead waiting on pool will invoke callback. when i test this thrift server,
it will crash. following is example
void TestAsyncService::Start()
{
    mSyncThreadPool.reset( new stone::ThreadPool(
                    100,
                    stone::ThreadPool::DynamicWorker,
                    "TestAsyncService"));
    mSyncThreadPool->Startup();
}

TestAsyncService::~TestAsyncService()
{
    try{
        mSyncThreadPool->Shutdown();
    }   
    catch(...)
    {   

    }   
}

void TestAsyncService::SayHello(std::tr1::function<void(std::string const& _return)> cob, const std::string& request)
{
    TestReqEntityPtr entity(new TestReqEntity());
    entity->req = request;
    entity->cob = cob;

    stone::ThreadPool::Callback* task = stone::NewClosure(
                this, 
                &TestAsyncService::DoRequest, 
                entity);

   if (mSyncThreadPool->AddTask(task) < 0)
   {   
       cout << "add task failed" << endl;
   }   

void TestAsyncService::DoRequest(TestReqEntityPtr req)
{
    cout << "req: " << req->req << endl;
    req->cob(req->req + string("back"));                                                                                                                     
}
int main()
{

    int retry = 30;
    while(1)
    {
        try{
            boost::shared_ptr<TestAsyncService> handler(new TestAsyncService());
            handler->Start();

            boost::shared_ptr<TAsyncProcessor> proc(new TestServiceAsyncProcessor(handler)); 
            boost::shared_ptr<TProtocolFactory> pfact(new TBinaryProtocolFactory());
            boost::shared_ptr<TAsyncBufferProcessor> bufproc(new TAsyncProtocolProcessor(proc, pfact));
            boost::shared_ptr<TEvhttpServer> server(new TEvhttpServer(bufproc, 6888));

            server->serve();
        }
        catch(std::exception& e)
        {
            cout << "server exception: " << e.what() <<endl;
        }
        catch(...)
        {
            cout << "server unkown exception: " << endl;
        }

        usleep(100*1000);
        if(retry-- <= 0)
        {
            cout << "exit from server: " << retry << endl;
            break;
        }
    }

    return 1;
}          

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message