the problem about boost trim function in multi-thread

I use boost trim function and find it perform very well in single-thread
environment.
But when I call trim function in multi-thread environment, it will have a
poor performance.
I also find that it will have a good performance when call it using
multi-process method.
In the end, I write a simple trim function, it perform very well in
multi-thread environment or multi-process environment.
I think I must use it incorrectly in multi-thread environment.
So I want to know what's wrong.
Thanks for any reply.
boost version: boost 1.46.1
os: linux redhat 6.1, 8core, 24G memory.
the blow is sample code
test1.cpp, call trim function in multi-thread environment
//----------------------------
---------------------
using namespace std;
using namespace boost;
void *TrimNString(void *arg) {
string base ="fdsffdsafdsa";
for(int i = 0; i != 50000000;i++)
{
string str = base;
trim(str);
}
return 0;
}
int main()
{
//8 threads to call trim function
system("date");
pthread_t mythread1, mythread2, mythread3, mythread4, mythread5,
mythread6, mythread7,mythread8;
pthread_create(&mythread1, NULL, TrimNString, NULL);
pthread_create(&mythread2, NULL, TrimNString, NULL);
pthread_create(&mythread3, NULL, TrimNString, NULL);
pthread_create(&mythread4, NULL, TrimNString, NULL);
pthread_create(&mythread5, NULL, TrimNString, NULL);
pthread_create(&mythread6, NULL, TrimNString, NULL);
pthread_create(&mythread7, NULL, TrimNString, NULL);
pthread_create(&mythread8, NULL, TrimNString, NULL);
pthread_join(mythread1, NULL);
pthread_join(mythread2, NULL);
pthread_join(mythread3, NULL);
pthread_join(mythread4, NULL);
pthread_join(mythread5, NULL);
pthread_join(mythread6, NULL);
pthread_join(mythread7, NULL);
pthread_join(mythread8, NULL);
system("date");
return 0;
}
test2.cpp, call trim function in multi-process environment
//-------------------------------------------------
/*
* test.cpp
*
* Created on: 2012-6-19
* Author: root
*/
#include

I use boost trim function and find it perform very well in single-thread environment.
But when I call trim function in multi-thread environment, it will have a poor performance.
Perhaps, it's because boot::trim() uses std::locale(), which involves a lot of locks in a multi-threaded environment. But try analysing it with a performance profiler.

On Jun 19, 2012, at 7:39 AM, Igor R wrote:
I use boost trim function and find it perform very well in single-thread environment.
But when I call trim function in multi-thread environment, it will have a poor performance.
Perhaps, it's because boot::trim() uses std::locale(), which involves a lot of locks in a multi-threaded environment. But try analysing it with a performance profiler.
That should be really easy to test. How does your test code perform if you change it to this: void *TrimNString(void *arg) { string base ="fdsffdsafdsa"; locale loc = locale(); for(int i = 0; i != 50000000;i++) { string str = base; trim(str, loc); } return 0; } Note also that trim calls the locale to look for whitespace, which is more flexible than your test for " \r\n\t". That's probably a significant performance difference, too. -- Marshall Marshall Clow Idio Software mailto:mclow.lists@gmail.com A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
participants (3)
-
Hu Jing
-
Igor R
-
Marshall Clow