This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Change default allocator?


Benjamin Kosnik wrote:

> > Is there _any_ way to change the default allocator used by libstdc++
> > (other than __USE_MALLOC)?
>
> Usually there is a default Allocator argument as a template parameter.
> Just use pthread_alloc instead of allocator.

Yes, but we would like to change the default allocator _without_ specifying
this every time we create a new string...

>
>
> > We have finally found the "reason" for the slowdown in our application
> > and the use of pthread_alloc seems to help alot, therefore we would like
> > to use this as default and specifically set the default alloc on
> > globally "shared" variables.
>
> Can you be a little less vague about said "reason?"

Sorry, there was a discussion about STL/__USE_MALLOC/etc that I initiated a
couple of days ago, and it turns out that doing the following generates a
huge amount of system load...

#include <pthread.h>
#include <unistd.h>
#include <string>

using namespace std;

void* worker(void *)
{
        while (true)
        {
                string s;
                s="jala";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
        }
}

int main()
{
        pthread_t *thread;
        pthread_attr_t pthread_attr_default;
        pthread_attr_init(&pthread_attr_default);

        int i;
        for(i=0; i<16; i++)
        {
                pthread_create(thread, &pthread_attr_default, worker, NULL);
        }

        sleep( 1000 );

        return 0;
}

But when using the pthread_alloc it runs just fine...

#include <pthread.h>
#include <unistd.h>
#include <string>
#include <bits/stl_pthread_alloc.h>

using namespace std;

typedef std::__allocator<char, std::pthread_alloc> std_char_pthread_alloc;
typedef std::basic_string <char, std::char_traits<char>,
std_char_pthread_alloc > pthread_string;

void* worker(void *)
{
        while (true)
        {
                pthread_string s;
                s="jala";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
                s+="foo2";
        }
}

int main()
{
        pthread_t *thread;
        pthread_attr_t pthread_attr_default;
        pthread_attr_init(&pthread_attr_default);

        int i;
        for(i=0; i<16; i++)
        {
                pthread_create(thread, &pthread_attr_default, worker, NULL);
        }

        sleep( 1000 );

        return 0;
}


>
>
> Did you see the example code that was posted about this here:
> http://gcc.gnu.org/ml/libstdc++/2001-10/msg00209.html
>

Yupp - been there!

Thanks for all comments!

/Stefan

>
> If you can modify this simple test case to demonstrate your problem
> (shouldn't be too hard) the probability of intelligent discussion goes
> way up.
>
> -benjamin

--
Military intelligence is a contradiction in terms.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]