Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mcai2012 opened this issue Jan 26, 2016 · 1 comment
Open

Comments

@mcai2012
Copy link

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
Copy link

aidevr commented Nov 14, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants