[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pthreads and setstacksize()

From: Fares Abdullah
Subject: pthreads and setstacksize()
Date: Mon, 7 Oct 2002 15:36:51 -0700


I've had some problems with my application, which uses pthreads, that I've
narrowed down to a test case.

I have an executable and a shared library. I have pthread calls (which set
some thread attributes and create a thread) in my shared library which I
would like to make use of from my executable. Now the problem is that
depending on how they (the executable and the shared library) are built,
the thread stack size is ignored and set to the default 2 megabytes.

If I build my executable with the "-lpthreads" option, and my shared object
without it, then it ignores my stack size setting:
        gcc -shared -o shared_library.so shared_library.c
        gcc -lpthread -rdynamic -ldl -o caller caller.c

But if I build my shared library with the "-lpthreads" option, then it
creates a thread with the correct stack size:
        gcc -lpthread -shared -o shared_library.so shared_library.c
        gcc -rdynamic -ldl -o caller caller.c

I tested the amount of memory that is allocated by using "strace" like
        export LD_LIBRARY_PATH=.
        strace -ff -o log ./caller
        grep map log*

strace creates a log file for the executable and for each thread that is
created (whose filename it appends the process ID to). Then I do a grep for
all of the mmap calls to see how much memory has been allocated for each

Is it incorrect to do a "-lpthread" for the executable, but not for the
"so"? Why do the rest of the pthread functions work (like pthread_create())
when the results of pthread_attr_setstacksize() are ignored? Should the
pthread_attr_t variable be in the caller, and not in the so? Is this a bug
in gcc or pthreads?

Another question: Is the memory that is allocated (via the mmap calls) ever

Any help is greatly appreciated.


Fares Abdullah

Here is the source to shared_library.c and to caller.c:

/* shared_library.c */
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
int create_pthread(pthread_t * thread, unsigned int stacksize, void *
entrypoint, void * entryarg)
    pthread_attr_t attr;
    if( pthread_attr_init( &attr ) != 0 ) {
        perror( "attr init" );
        exit( 1 );
    if( pthread_attr_setstacksize( &attr, stacksize) != 0 ) {
        perror( "set stack size" );
        exit( 1 );
    if( pthread_create( thread, &attr, entrypoint, entryarg ) != 0 ) {
        perror( "thread create" );
        exit( 1 );
    return 0;

/* caller.c */
#include <stdio.h>
#include <dlfcn.h>
#include <pthread.h>
void* thread_func( void* arg )
   printf( "arg = %s, %s()\n",arg, __FUNCTION__ );
    return 0;
int main() {
    void *fnHandle = NULL;
    pthread_t thread;
    char* x;
    int (*fnAddress)(pthread_t* handle, unsigned int stacksize, void *
entrypoint, void* entryarg);
    x = "called!";
    fnHandle = dlopen("shared_library.so" , RTLD_NOW);
    if (NULL == fnHandle) {
        printf("dlopen failed\n");
        fputs (dlerror(), stdout);
        return 1;
    fnAddress = dlsym(fnHandle, "create_pthread");
    if (NULL == fnAddress) {
        printf("dlsym failed\n");
        fputs (dlerror(), stdout);
        return 1;
    (fnAddress)(&thread, 16*1024, thread_func, x); // stack size set to 16k
    return 0;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]