
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<pthread.h> #include <boost/algorithm/string.hpp> #include <string> #include <vector> #include <cstdlib> #include <iostream> using namespace std; using namespace boost; void *TrimNString(void *arg) { system("./TrimNString");// TrimNString is produced by test3.cpp return 0; } int main() { //8 process 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; } test3.cpp, the executable file for test2.cpp /* * test.cpp * * Created on: 2012-6-19 * Author: root */ #include<pthread.h> #include <boost/algorithm/string.hpp> #include <string> #include <vector> #include <cstdlib> #include <iostream> using namespace std; using namespace boost; //produce the executable file int main() { string base ="fdsffdsafdsa"; for(int i = 0; i != 50000000;i++) { string str = base; trim(str); } return 0; } test4.cpp, call a simple trim(not boost library) function in multi-thread environment, it has similary performance like multi-process calling. //------------------------------------------------- #include<pthread.h> #include <boost/algorithm/string.hpp> #include <string> #include <vector> #include <cstdlib> #include <iostream> using namespace std; using namespace boost; void ltrim(string & str) { if(str.find_first_not_of(" \n\r\t") != string::npos) { str = str.substr(str.find_first_not_of(" \n\r\t")); } } void rtrim(string & str) { if(str.find_first_not_of(" \n\r\t") != string::npos) { str = str.substr(0, str.find_last_not_of(" \n\r\t") + 1); } } void trimStr(string &str) { ltrim(str); rtrim(str); } void *TrimNString(void *arg) { string base ="fdsffdsafdsa"; for(int i = 0; i != 50000000;i++) { string str = base; trimStr(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; }