Producer consumer architecture

H

hg

As a part of a TCP/IP server I want to split up the incoming socket
requests from the work queue.
To do this I have one thread which figures out which sockets are
pending to be queried and another set of workerthreads which actually
does this work.

So far I've used std::list as a means of pushing a request from the
socket thread to the worker threads.
Each worker thread would then look every 10 ms to see if there is a
job in the queue.

Now I'm facing a problem though as there can be more requests of the
same job coming from the socket thread.
This has lead to concurrency problems when closing sockets (e.g. one
worker thread detects the socket is done communicating and next one
believes there is still action to perform).

Therefore I want to remove duplicates when adding jobs to the work
queue.
So I'm looking for a replacement container which will tell me rather
fast if a key element already exists and if preferedly still have this
FIFO behavior of std::list.
It's not unusual that there can be several thousands (e.g. up to
10000) objects in the worker queue.

Should I aim for anything better than std::list? The only thing list
won't give me is fast duplicate handling.

Thanks.

-- Henrik
 
V

Victor Bazarov

As a part of a TCP/IP server I want to split up the incoming socket
requests from the work queue.
To do this I have one thread which figures out which sockets are
pending to be queried and another set of workerthreads which actually
does this work.

So far I've used std::list as a means of pushing a request from the
socket thread to the worker threads.
Each worker thread would then look every 10 ms to see if there is a
job in the queue.

Now I'm facing a problem though as there can be more requests of the
same job coming from the socket thread.
This has lead to concurrency problems when closing sockets (e.g. one
worker thread detects the socket is done communicating and next one
believes there is still action to perform).

Therefore I want to remove duplicates when adding jobs to the work
queue.
So I'm looking for a replacement container which will tell me rather
fast if a key element already exists and if preferedly still have this
FIFO behavior of std::list.
It's not unusual that there can be several thousands (e.g. up to
10000) objects in the worker queue.

Should I aim for anything better than std::list? The only thing list
won't give me is fast duplicate handling.

What if you split the job of keeping the order and preventing duplicates
between, say, std::list and std::set? Let your std::set store the
iterators to the std::list of objects. Make sure the comparator for
the std::set is implemented properly (it can be tricky since the functor
isn't returning the [in]equality but rather the less-than relationship).

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top