473,473 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

boost:threads tweaks?

8 New Member
Hi All,

I am converting some C code into C++ code. The objective is to improve throughput.

I have some code written in C which serially parses through a list of files[~10000], opens each one of them, processes the data and closes the file. All the files are processed one by one.

The obvious performance bottleneck that i could think of is the wasted cpu cycles for file i/o.

*My solution* was to spawn multiple threads to do the file i/o. For around ~10000 files i spawned around 10 threads. All the file names are in one stl container [deque used as queue]. The threads process the files from the container in a round-robin fashion. So whenever a thread finishes processing a particular file it gets another one from the container untill the container is empty

I used the boost::thread_group to manage the 10 threads.

However i notice that after i changed the code by introducing boost::threads and an stl container the performance drops and the code runs slower than the version without multiple threads.

some details:
size of each file: approx 2kb
boost version : 1.32
compiler: msvc 7.1
cpu: intel 4 core, 3.20 ghz

I have made a concious effort to keep the critical sections to the minimum and to make the code as efficient as possible [by me]

Is there some way in which the boost::thread_group can be tweaked by passing config params etc? I could not find anything relevant in the boost documentation for 1.32.

Thanks!
Mar 17 '09 #1
5 3567
JosAH
11,448 Recognized Expert MVP
You have four cores; what happens when you create at most four threads?

kind regards,

Jos
Mar 17 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
What JosAH says is correct. With 4 cores you can run only 4 threads. But you say all of your threads are accessing the same STL container. That means there are lots of context switches as these threads block each other try to access a common resource.

The general rule of multithreading is that is runs slower than a single thread unless you have separate cores for each thread AND there are no shared resources.

As a result I am not surprised that youre multuthreaded program is running slower than a single threaded one.
Mar 17 '09 #3
ameyav
8 New Member
The STL container contains a list of file names.
The only time any thread accesses the container is to retrieve the next file name in the queue. This part being the only critical section where threads could block.
After it retrieves a filename from the container it accesses no other shared resource.
I assumed there would be some performance improvement if 4 threads would work on 4 separate file i/o in parallel.

I also assumed the performance gain achieved from doing file i/o in parallel will far surpass the performance degradation due to some blocked calls on the shared container.

some time stats: [i have taken the most reproducable time for each thread configuration. Also took care that the CPU load would not increase due to some other process during the tests]

Original C code w/o MT : 0.22
C++ code with 1 thread : 0.28
C++ code with 2 threads : 0.26
C++ code with 3 threads : 0.25
C++ code with 4 threads : 0.25
C++ code with 6 threads : 0.25
C++ code with 10 threads : 0.25

Looks like the performance has hit a plateau after the initial gain. I am sure it will start degrading if i increase the number of threads.
Mar 18 '09 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Yep. Rule 1 of multithreading: The program runs slower.

The only relief is true parallel processing where you have multiple cores.

These threads are just tripping over each other trying to access that container.

HINT: Is this STL container constant? That is, once filled is it unchanged for the life of the program? If so, remove your critical sections since it won't cause a problem if multiple threads just READ the container. However, if there is an update of any kind, then leave those critical sections alone,.
Mar 18 '09 #5
ameyav
8 New Member
hmmm .. thanks for your help, will try to play around a bit with the design and check for results.
Mar 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
4
by: Lighter | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; using namespace boost; void hello() { cout << "Hello world, I'm a thread!" << endl; }
9
by: Peter v. N. | last post by:
Hi all, First off: consider this to be a low priority thread! This may sound heretical: but I'd like to know what the community thinks about using Boost extensively in C++ projects (mainly...
8
by: Matt England | last post by:
My team currently using Boost Threads, but we are considering switching to ZThreads. (We seek cross-platform, C++ multithreading capabilities in an external library.) ZThread(s): ...
13
by: barcaroller | last post by:
If this is not the right newsgroup for "C++ threads", please consider this message off-topic and ignore it. --------------------------------------------- I am writing a multi-threaded application...
1
by: Chris Roth | last post by:
I've been working with boost::threads to do some multithreading in my code and have run into some questions that I haven't been able to find answers to. I'll include some sample code to...
5
by: petru.marginean | last post by:
Hello, I would like to re-implement this function that currently uses pthreads: pthread_mutex_t mutex; int i = pthread_mutex_init(&mutex, 0); void MutexCB(int lock) {
10
by: Harsha | last post by:
Hi..... I need to develop a program for my academic project using graphics in c++. I want to do it using THREADS so that it can have mouse interface while accepting input...I want to know how to...
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.