Multi Threading

Discussion about everything. New games, 3d math, development tips...
Post Reply
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Multi Threading

Post by Alpha Omega »

I am writing a multi-threaded application that combines networking and irrlicht. I understand that certain things need to thread safe. I want to use iostream and fstream from C++ STL compiling under Visual Studios 2010 Express using the MSVC compiler. I have gone to http://msdn.microsoft.com/en-us/library/c9ceah3b.aspx which says that...
The iostream classes follow the same rules as the other classes, with one exception. It is safe to write to an object from multiple threads.
Now it does not say anything about fstream therefore I understand it needs to be thread safe. Should I trust Microsoft and believe that iostream is thread safe? Portability is a big deal with networking and I only want the best for my beloved Irrlicht. So should I write a lockout class with some type of a buffer to prevent all reads and writes of iostream and fstream variables together or just for fstream?

Also what is the best implementation for good performance. I am writing a server-client program using TCP/IP as a connection. I want the server to be fast so clients can send alot of data to it. Anyone know a good read?
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: Multi Threading

Post by zerochen »

hi,

whole quote:
[quote]The iostream classes follow the same rules as the other classes, with one exception. It is safe to write to an object from multiple threads. For example, thread 1 can write to cout at the same time as thread 2. However, this can result in the output from the two threads being intermixed.
quote]

if i understand it right it is safe to write but your result is not safe
so it is not thread safe
for example:
thread 1 writes: Im thread 1
thread 1 writes: Im thread 2

the result can be: Im thIm thread 2read 1

alternativ you can use irrNetLite 2 somewhere in the project announcements forum
it is very good in my opinion

cu
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Re: Multi Threading

Post by Alpha Omega »

Thanks for the reply,

Yes I did find this to be the case so I restructured my program so the console thread was the only thread allowed to use cout and then had all the worker threads right there own log file if they needed to express a saying. I also used a Mutex whenever I needed to access data between threads. It ended up working great.

I knew of irrnet lite but I believed it to lack functionality so I decided to go with a functional C approach. I may wrap the code into a C++ style class like irrnet lite, maybe I should release the code to compare side by side later on.
Brainsaw
Posts: 1183
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Re: Multi Threading

Post by Brainsaw »

IIRC IrrnetLite doesn't do any threading, you would have to implement that by yourself (one of the major points on my ToDo list).
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: Multi Threading

Post by zerochen »

yes you are right but in the irrnetlite post there is a good example that uses threads
SGH
Posts: 13
Joined: Wed Aug 24, 2011 2:53 pm

Re: Multi Threading

Post by SGH »

What about RakNet? it's so simple.
Post Reply