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]

libstdc++ multi-threading: std::vector resize crash


Hi all,

I have the problem with libstdc++ memory allocation which I'm fighting against
the last 2 weeks. Before writing directly to mailing list I tried to
more or less thoroughly read through existent messages however this
didn't help me a lot.

The problem is the following: I work in EXACUS project and my server
program should process multiple requests from clients (CGI-scripts)
simultaneously. Without getting deeper into details, I pasted a piece
of code where it crashes.

This code might look meaningless because I tried to remove everything which
does not affect the segfault. The point is the following: the
server function "run()" waits for client requests and spawns new child
threads. A child thread "compute_thread()" should carry out some useful job which heavily
uses STL. But note that it DOES NOT use any global or static variables
- only stack variables.

The program constantly crashes if mutexes are not used: for
demonstrative purposes I left only an infinite loop inside which
vector::resize is repeatedly called on a LOCAL variable (this is a
place where it crashes). gdb reports: SIGSERV in mallopt() from
/lib/tls/libc.so.6. However, is you comment line
'p.resize(degr+1, NT(0))' this will work.

Or if you uncomment pthread_mutex_lock/unlock lines this will also
work fine (because, probably, a local variable 'pp' is destroyed before
another thread interrupts execution).

As I understood, this has to deal with STL memory allocation. So,
previously I used a default allocator and, then, tried mt_alloc but
result is the same.

I use gcc 3.4.5 compiler with libstdc++v3. gcc -v:
 --enable-shared --enable-threads=posix --enable-__cxa_atexit
 --with-system-zlib --enable-nls --without-included-gettext --enable-clocale=gnu
 --enable-debug

 and compile my source code with '-pthread' and '-lpthread' options.
 System: 2.6.15.6.4.p4-smp i686 GNU/Linux

Thanks in advance for any help!!
here is the code:

inline static void swallow(std::istream &is, char d) {
    char c;
    do { c = is.get(); } while (isspace(c));
    if (c != d) { 
        std::cerr << "input error: unexpected character in polynomial\n";
    }
}

typedef CORE::BigInt NT;
typedef std::vector<NT, __gnu_cxx::__mt_alloc<NT> > VI;

void input_ascii(VI& p, std::istream &is) {
    int degr;
    swallow(is, 'P');
    swallow(is, '[');
    is >> (degr);
    // << HERE SEGFAULT OCCURS
    p.resize(degr+1, NT(0));
    // << HERE SEGFAULT OCCURS
}

static int func(char *poly)
{
    VI pp;
    std::stringstream str(poly);

    input_ascii(pp, str);
    return 1;
}

// this is a thread function
void *compute_thread(IPC_Message *pmsg)
{
    ...
    shm_addr = (char *)shmat(shm_id, NULL, 0))
    std::cout << "address: " << (void *)shm_addr << std::endl;
    while(1){
       // << UNCOMMENT mutex lock/unlock to avoid segfault
       // pthread_mutex_lock(&func_mutex);
       func(shm_addr);
       // pthread_mutex_unlock(&func_mutex);
    }
    return NULL;
}

// the main thread function used to spawn child threads
void CGI_Server::run()
{
    while(1) {
        std::cout << "Waiting for clients..." << std::endl;
        IPC_Message ipc_msg;
        msgrcv(mq_id, &ipc_msg, sizeof(ipc_msg)-4, MSGS_SERVER, 0);
        
        if(ipc_msg.m_type == ...) {
              pthread_t child;
              pthread_create(&child,NULL,(THREAD_PROC)compute_thread, &ipc_msg));
         }
    }
}

-- 
Best regards,
 Pavel                          mailto:asm@mpi-sb.mpg.de


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