Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: evilrixPosted on 2009-10-26 at 10:27:04ID: 25664576
The problem you have is that as with any multi-threaded application if you are accessing shared resources you have to be sure it's thread safe. Accessing a stream in a multi-threaded way will not work as you want because the getline is not atomic in this respect. If the thread context switches whilst getline is part way through reading a line the other thread will start reading where this left off. What you can do is provide mutual exclusion semantics (using a boost mutex) so only one thread can access and read the stream at a time but this means each thread is only able to serially process the stream so it probably won't make things any faster, just more complex.
The rule of thumb is if you think you need to use threads, think again. If you still think you do then check one more time. If, on the third check you conclude you do then proceeded... carefully.