N
nan.li.g
Hello, all,
I have an interesting problem about stl map and pthread on Linux
and g++. The source code is as follows.
//mt_map_test.cpp
#include <string>
#include <map>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
using namespace std;
static void* thrd_work( void* data )
{
long count = reinterpret_cast<long>( data );
const int SIZE =32;
char buf[SIZE];
memset( buf, 'h', SIZE );
map<string, string> strMap;
for ( long i=0; i< count ; i++ ) {
char key[8];
sprintf( key, "%d", i );
strMap[ key ] = buf;
}
}
int main()
{
int JOB_NUM = 320000;
pthread_t tid[THRD_NUM];
for ( int i=0; i< THRD_NUM; i++ ){
pthread_create( tid+i, NULL, &thrd_work,
reinterpret_cast<void*> (JOB_NUM) );
}
for ( int i=0; i< THRD_NUM; i++ ){
pthread_join( tid, NULL );
}
}
And here is what I got from my workstation ( Dual AMD Opteron machine,
RHEL 3)
[nan@eudyptula test]$ for t in 1 2; do g++ -DTHRD_NUM=$t
mt_map_test.cpp -lpthread ; time ./a.out; done
real 0m1.390s
user 0m1.280s
sys 0m0.120s
real 0m3.450s
user 0m5.320s
sys 0m1.170s
I expected that the 2 times should be roughly equal. But clearly I
experienced significant slowdown with 2 threads.The same also happened
to a dual Intel Xeon machine. I suspect the internal stl map
implementation is improper.
I've spent hours googling without any answer. I really need advice from
a C++ expert. Thanks a lot.
Nan
I have an interesting problem about stl map and pthread on Linux
and g++. The source code is as follows.
//mt_map_test.cpp
#include <string>
#include <map>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
using namespace std;
static void* thrd_work( void* data )
{
long count = reinterpret_cast<long>( data );
const int SIZE =32;
char buf[SIZE];
memset( buf, 'h', SIZE );
map<string, string> strMap;
for ( long i=0; i< count ; i++ ) {
char key[8];
sprintf( key, "%d", i );
strMap[ key ] = buf;
}
}
int main()
{
int JOB_NUM = 320000;
pthread_t tid[THRD_NUM];
for ( int i=0; i< THRD_NUM; i++ ){
pthread_create( tid+i, NULL, &thrd_work,
reinterpret_cast<void*> (JOB_NUM) );
}
for ( int i=0; i< THRD_NUM; i++ ){
pthread_join( tid, NULL );
}
}
And here is what I got from my workstation ( Dual AMD Opteron machine,
RHEL 3)
[nan@eudyptula test]$ for t in 1 2; do g++ -DTHRD_NUM=$t
mt_map_test.cpp -lpthread ; time ./a.out; done
real 0m1.390s
user 0m1.280s
sys 0m0.120s
real 0m3.450s
user 0m5.320s
sys 0m1.170s
I expected that the 2 times should be roughly equal. But clearly I
experienced significant slowdown with 2 threads.The same also happened
to a dual Intel Xeon machine. I suspect the internal stl map
implementation is improper.
I've spent hours googling without any answer. I really need advice from
a C++ expert. Thanks a lot.
Nan