Skip to content

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

Description

@mcai2012

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions