Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:16:41+00:00 2026-06-06T04:16:41+00:00

I use boost trim function and find it perform very well in single-thread environment.

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:16:42+00:00Added an answer on June 6, 2026 at 4:16 am

    You are not supplying a locale so trim will use the current locale. Fetching the current locale probably need a lock which might cause your performance problem.

    try to create the locale once and use it in all trim calls

    std::locale mylocale = std::locale();
    
    ...
    
    trim(str, mylocale);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently wanted to use boost::algorithm::join but I couldn't find any usage examples and
I wanted to use boost::thread in my program, but get the following compiler error
I'm trying to use boost::thread but I can't compile because of an error compiling
Is it possible to use boost::fusion::invoke function to call a function that has default
I want to use boost::crc so that it works exactly like PHP's crc32() function.
I use boost::signal with different function signatures and different combiners. In a class that
We use Boost statically linked with our app but now I want to use
I am trying to use boost::bind with a boost::function using this. It seems a
I tried to use Boost library but I failed, see my code: #include listy.h
I'm learning the use of boost smart pointers but I'm a bit confused about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.