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]
Other format: [Raw text]

Is __default_alloc_template thread safe on Solaris?


I have extensively used libstdc++ on multithreaded apps on linux platform, however STLs seem to have notorious behaviour on Solaris platform. Following test program repeatedly crashes on Solaris but was not able to recreate it on linux. g++ verion on Solaris is 2.95.3 while on linux it is 2.96. libstdc++ was built with pthread enabled on both platforms.The code was compiled with following options -lpthread -D _REENTRANT

#include <iostream>
#include <map>
#include <pthread.h>
#include <stdio.h>

const char *Months [12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
FILE *pFile = NULL;
pthread_mutex_t Mutex = PTHREAD_MUTEX_INITIALIZER;


using namespace std;

void *Loop(void *)
{
	while(1)
        {
        	map<int, const char *> *pMap = new map<int, const char *>;

                if(pMap == NULL)
                {
                        fprintf(stdout, "map creation failed\n");
                        continue;
                }

for(int i = 1; i < 13; i++)
pMap->insert(map<int, const char *>::value_type(i, Months[i-1]));


pthread_mutex_lock(&Mutex);

for(map<int, const char *>::iterator ite = pMap->begin(); ite != pMap->end(); ite++)
fprintf(pFile, "Thread : %d -- Month : %d : %s\n", pthread_self(), ite->first, ite->second);


pthread_mutex_unlock(&Mutex);

pMap->clear();

                delete pMap;
        }
}

int main (int argc, char **argv)
{
        pthread_t thread1, thread2;

pFile = fopen("log.txt", "w");

        fprintf(stdout, "creating threads\n");
        if(pthread_create(&thread1, NULL, Loop, NULL) == 0)
                fprintf(stdout, "thread 1 successfully created\n");

        if(pthread_create(&thread2, NULL, Loop, NULL) == 0)
                fprintf(stdout, "thread 2 successfully created\n");

        while(1)
                ;

        return 0;
}

Following is core details

(gdb) bt
#0 0x14954 in __default_alloc_template<false, 0>::allocate (__n=24)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_alloc.h:422
#1 0x145f4 in simple_alloc<_Rb_tree_node<pair<int const, char *> >, __default_alloc_template<false, 0> >::allocate (__n=1)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_alloc.h:228
#2 0x13f3c in _Rb_tree_alloc_base<pair<int const, char *>, allocator<char *>, true>::_M_get_node (this=0x259a8)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_tree.h:475
#3 0x134e4 in _Rb_tree<int, pair<int const, char *>, _Select1st<pair<int const, char *> >, less<int>, allocator<char *> >::_M_create_node
(this=0x259a8, __x=@0xff07bf78) at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_tree.h:554
#4 0x13738 in _Rb_tree<int, pair<int const, char *>, _Select1st<pair<int const, char *> >, less<int>, allocator<char *> >::_M_insert (
this=0x259a8, __x_=0x0, __y_=0x26080, __v=@0xff07bf78)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_tree.h:842
#5 0x13abc in _Rb_tree<int, pair<int const, char *>, _Select1st<pair<int const, char *> >, less<int>, allocator<char *> >::insert_unique (
this=0x259a8, __v=@0xff07bf78) at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_tree.h:894
#6 0x14134 in map<int, char *, less<int>, allocator<char *> >::insert (this=0x259a8, __x=@0xff07bf78)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_map.h:162
#7 0x1282c in Loop () at thread-test.cpp:28



(gdb) info threads
3 LWP 2 0x14954 in __default_alloc_template<false, 0>::allocate (__n=24)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_alloc.h:422
2 LWP 1 main (argc=1, argv=0xffbff534) at thread-test.cpp:74
* 1 LWP 3 0x14954 in __default_alloc_template<false, 0>::allocate (__n=24)
at /opt/sfw/lib/gcc-lib/sparc-sun-solaris2.9/2.95.3/../../../../include/g++-3/stl_alloc.h:422



Looking at the __default_alloc_template indicates that it's thread-safe provided that __STL_PTHREADS which is depended on _PTHREADS is defined,
but I was not able to figure out where _PTHREADS was defined. Can someone clarify this issue? Or am I missing something here?


thanks in advance,

dhammika


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