C++ Socket / Threading Advice

Soldato
Joined
7 Apr 2004
Posts
4,212
Hi,

Imagine that i have a vector container, which contains a list of items that need downloading, and i want X amount of these items to download simultaneously.

These items are being transfered over a custom protocol, not something standard like http so i cant use a pre made library, rather im dealing with raw socket operations.

How do i create a decent stable architecture for this purpose?

So my vector is going be the thread safe queue object so to speak, that my download objects will access, then process the item from the vector.

Problem is whats the best design approach to do this?

I have been looking at the Boost library for threading, but some people have said i can just use asynchronous I/O and eliminate the need for threads, but i have no idea how i would implement asio?

My idea for threading would be to have 1 socket / download object per thread, and each thread would lock/unlock the vector queue when required. Would this work? or is it over the top?

I'm really just after a good understanding of the design so i can implement it as hassle free as possible because threading is scary :p.

This is for linux, so does anyone know if theres any existing code for this type of design i could use, or look at for ideas, any library's that exist?

Thanks for any help,

Jason
 
Perhaps you could lock access to the individual item in the vector container when you pass it to your thread for downloading.

I don't have my networking programming book handy so i can't check the ASync I/O stuff but it's perfectly possible - you set off the action and then wait on the handle/event to be notified.

Threading isn't that scary...honest! All you have to do is make sure all memory accesses are thread safe - in your thread class have 'set/get' functions ie. if you want to set a variable in the execute loop use a function in the classes which uses an arguement to set the value.
 
Back
Top Bottom