views:

188

answers:

5

I'm trying to implement my own IRC client as a personal proejct and I realized I needed a way to read and write from the socket at the same time. I realized I could have a reading thread which reads from the socket in the background and puts data in a queue and I could have another thread which writes data from a queue to the socket. However I have no idea on how to start with multithreaded programing or how to do it with c++. Where do I go from here?

+4  A: 

It's specific to C and *nix, but I can't think of a better starting place than Beej's Guide to Network Programming. "You will learn from the Jedi Master who instructed me."

You'll learn the basics of reading and writing to sockets, and more importantly, that multi-threading isn't necessarily the right answer.

Kristo
Wait.. what does that have to do with threading?
thyrgle
@thyrgle, I'm answering the "implement an IRC client in C++" question that was semi-hidden in the OP. I *am* geniunely trying to be helpful here. The text of the question implies that the OP is very early in the learning phase on this material. It's hard to ask the right question at that stage.
Kristo
+9  A: 

For C++ threads, boost::thread (which is the basis for the upcoming std::thread) is the best way to go. That said, while threads might be the correct solution for your particular case, I just wanted to throw it out there that select and non-blocking sockets are a common approach to interleaving the reading/writing and writing of multiple sockets without the need for threads. The boost::asio library wraps the functionality of select and non-blocking sockets in a cross-platform, C++ manner.

Michael Aaron Safyan
Once there are boost.threads, it's only a small step to boost.asio which would handle those sockets for OP too.
Cubbi
@Cubbi, good point... I will update my post to note that.
Michael Aaron Safyan
+1 for asio. Definitely easier to get right than threading.
Ben Voigt
Thanks for the information about boost::thread and non-blocking sockets.
zoke
+3  A: 

I would suggest using Qt Threading. It is highly documented with really excellent sample code on almost every feature. Plus they are LGPL licensed now and will run on most every platform and include the source code with the binaries. They also have very good network supoort.

Whatever way you choose, make sure that they have good documentation and samples

photo_tom
A: 

I'd suggest looking at the POCO libraries. In my opinion they are easier to get on with than boost and have excellent documentation. These libs provide great frameworks for writing multithreaded networking code. You can learn a lot from them and get up and running pretty quickly.

Huw
A: 

I suggest ACE. It has portable abstractions for many operating system functions (*nix, Windows etc): BSD sockets, Threads, Mutexes, Semaphores etc - write once compile anywhere (See ACE_OS namespace of ACE).

It has a lot of network application patterns you can use (ACE_Reactor would be good for the beginning) but you can use the portable abstractions of the BSD functions (socket, send, recv, close, select - they are enough for your IRC client)

As previously mentioned boost is also an option and usually any cross-platform library providing portable abstractions for each operating system (I can think of wxWidgets, qt for the graphical part - if you want to do this).

And one advice: do not use threads unless you really need to. They are not as easy as it seems.

When referring to the network communication I believe that what you want to do is easily achievable in a single threaded application(ACE_Reactor helps you a lot here but you are free to use the BSD socket functions). First understand how sockets work, then - if you want to - understand how the reactor makes use of sockets in its network application patterns(ACE_Reactor works in conjuction with ACE_Event_Handler objects).

Hope it helps!

Iulian Şerbănoiu