luca3m / redis3m

A C++ Redis client
Apache License 2.0
190 stars 78 forks source link

Issue: multiple threads can't put the connection back to the pool #41

Open mcai2012 opened 8 years ago

mcai2012 commented 8 years ago

I looked at the simple_pool example and the following is my code. Basically what I am trying to do is to create two threads and each of them pushes 500 messages to the server.

void produce(simple_pool::ptr_t pool, int requestPerThread, std::string& nv, int idx)
{
    try
    {
        connection::ptr_t conn = pool->get();

        std::string queue = "queue1";

        if (idx % 2 == 0)
        {
            queue = "queue2";
        }
        for (int i = 0; i < requestPerThread; i++)
        {
            reply r = conn->run(command("RPUSH") << queue << nv);
        }
        std::cout << "thread " << idx << " finished" << std::endl;
        pool->put(conn);
    }
    catch(const transport_failure& ex)
    {
        std::cout << "having transport failure" << std::endl;
    }
}

int main(int argc, char **argv)
{

    int threadCount = 2;
    int requestCount = 1000;
    int requestPerThread = requestCount / threadCount;

    std::string nv = "some long string"

    simple_pool::ptr_t pool = simple_pool::create("10.2.21.28");

    std::vector<Concurrent::JoiningThread> threads;
    threads.reserve(threadCount);

    for (auto idx = 0; idx < threadCount; ++idx)
    {
        threads.emplace_back("name", [&, idx](){produce(pool,requestPerThread, nv, idx);  });
    }

    for (auto & thread : threads)
    {
        thread.join();
    }  
    return 0;
}

When I run the above code, about 30% to 40% of the time, both threads can finish pushing 500 messages to the server and put the connection back to the pool and return successfully. However, the majority of the time, there is one thread that can't finish and never put the connection back to the pool. It gets stuck after the thread sent around 495 messages. The program doesn't terminate but it also doesn't do anything after that.

If I change the threadCount to 1, it succeeds every time and put the connection back to the pool.

Could you please take a look and help me understand what is going on here? Thank you.

aidevr commented 8 years ago

Any update on this issue ? seems like i'm facing something similar.