Я использую функцию усиленной подстройки и считаю, что она работает очень хорошо в среде с одним потоком.О повышении эффективности функции обрезки триммера в многопоточной среде
Но когда я вызываю функцию trim в многопоточной среде, она будет иметь низкую производительность. Я также обнаружил, что он будет иметь хорошую производительность при вызове с использованием метода многопроцессорности.
В конце концов, я пишу простую функцию обрезки, она отлично работает в многопоточной среде или в многопроцессорной среде.
Я думаю, что я должен использовать его неправильно в многопоточной среде. Итак, я хочу знать, что случилось.
Спасибо за любой ответ.
boost версия: boost 1.46.1 os: linux redhat 6.1, 8core, 24G память.
удар пример кода test1.cpp, вызовите функцию обрезки в среде многопоточной
//----------------------------
---------------------
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, вызовите функцию обрезки в многопроцессной среде
//-------------------------------------------------
/*
* 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, исполняемый файл для 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.c pp, вызов простой функции обрезки (не форсированной библиотеки) в многопоточной среде, она имеет аналогичную производительность, например, многопроцессорный вызов.
//-------------------------------------------------
#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;
}
..looks больше как тест производительности строки CTOR, чем любая подрезка. Что сложного в обрезке строки, и, как только строка существует на самом деле, некоторый бит кода, который разделяет ведущие/конечные пробелы, не должен быть многопоточным. –
Я использую многие функции форсированной строки. Я не знаю, что другие функции алгоритма boost имеют одинаковую проблему в многопоточной среде. Если проблема только в триммерке, я могу заменить функцию обрезки. Но я не уверен в этом. – seacat
Вы пытались реализовать советы, которые вы получили от Boost ML? http://thread.gmane.org/gmane.comp.lib.boost.user/74528/focus=74532 Каковы ваши выводы? –