开发者

Running a Secondary Thread for Output in C++

开发者 https://www.devze.com 2023-02-17 12:10 出处:网络
I’m looking for a portable method for creating threads specifically for output of data in C++. I’d prefer to stay away from Boost if possible, but I’m not against using it if it’s the best option.

I’m looking for a portable method for creating threads specifically for output of data in C++. I’d prefer to stay away from Boost if possible, but I’m not against using it if it’s the best option.

Here is the situation: I have a program that does a complex computation on some data that it reads and produces three output streams with a large amount of textual data. These three streams are being compressed on the fly using the Bzip2 library. What I would like to do is to have the main computation run in the main thread, while the compression and output of the data is done in three additional threads. The idea being that in this way I can utilise the available computing cores and eliminate any bottleneck that the Bzip2 compression may be causing to the actual processing.

The way I imagine this working is for the three output threads to have open output file streams and to be waiting for string data that will then be compressed and output. The main thread will run its computation sendi开发者_C百科ng output to the other threads when necessary. Obviously, adequate buffering will have to be designed, but that’s not a problem.

I’d appreciate any suggestions regarding the best way to tackle this problem, in particular, what C++ libraries are the most appropriate for the task at hand. Keep in mind, that I would like to handle the buffering in the output threads and they should receive string class data.

Thanks in advance!


C++ doesn't support threads in its standard (at least not now), and to have threads portably you must use some library. There are many C++ libraries giving you portable threads out there, and your particular problem doesn't seem special in any way. Boost is very well received and adopted and has the best chance to influence future versions of the C++ standard. It is efficient and portable, so why not use it?


You should really use the Boost.Thread library. It is well documented, tested and light-weight (compared to full-featured libraries with multi-platform threading support, such as Qt).


Take a look at Boost ASIO: http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/overview/core/async.html

It's very flexible in terms of threading organization. As a matter of fact you may re-think your design and get rid of additional threads at all. But it also supports your idea as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号