POSIX thread question - Linux

This is a discussion on POSIX thread question - Linux ; Below is simple code of POSIX thread from a book: #include #include #include pthread_mutex_t cntr_mutex = PTHREAD_MUTEX_INITIALIZER; long protVariable = 0L;//LINE1 void *myThread( void *arg ) { int i, ret; for (i = 0 ; i ret = pthread_mutex_lock( &cntr_mutex ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: POSIX thread question

  1. POSIX thread question

    Below is simple code of POSIX thread from a book:


    #include
    #include
    #include

    pthread_mutex_t cntr_mutex = PTHREAD_MUTEX_INITIALIZER;

    long protVariable = 0L;//LINE1

    void *myThread( void *arg )
    {
    int i, ret;

    for (i = 0 ; i < 10000 ; i++) {

    ret = pthread_mutex_lock( &cntr_mutex );

    assert( ret == 0 );

    protVariable++;

    ret = pthread_mutex_unlock( &cntr_mutex );

    assert( ret == 0 );

    }

    pthread_exit( NULL );
    }

    #define MAX_THREADS 10

    int main()
    {
    int ret, i;
    pthread_t threadIds[MAX_THREADS];

    for (i = 0 ; i < MAX_THREADS ; i++) {
    ret = pthread_create( &threadIds[i], NULL, myThread, NULL );
    if (ret != 0) {
    printf( "Error creating thread %d\n", (int)threadIds[i] );
    }
    }

    for (i = 0 ; i < MAX_THREADS ; i++) {
    ret = pthread_join( threadIds[i], NULL );
    if (ret != 0) {
    printf( "Error joining thread %d\n", (int)threadIds[i] );
    }
    }

    printf( "The protected variable value is %ld\n", protVariable );

    ret = pthread_mutex_destroy( &cntr_mutex );

    if (ret != 0) {
    printf( "Couldn't destroy the mutex\n");
    }

    return 0;
    }

    It creates 10 threads. My questions are:
    1. The 10 threads share the same code. Do the 10 threads share both
    main() and myThread(), or just myThread()?

    2. The 10 threads share the same variables. Do the 10 threads share all
    the variables in the code? or only global variables?

    3. The variable protVariable (LINE1) is shared among the 10 threads.
    All the 10 threads can modify protVariable, right?

    4. I use the following command to compile it:
    gcc -o pt pthread.c

    The link error is:
    /tmp/cccuKYQv.o(.text+0xeb): In function `main':
    : undefined reference to `pthread_create'
    /tmp/cccuKYQv.o(.text+0x135): In function `main':
    : undefined reference to `pthread_join'
    collect2: ld returned 1 exit status

    What is the problem?

    Thanks.


  2. Re: POSIX thread question

    On 28 Aug 2006 14:15:21 -0700, Jack wrote:
    > Below is simple code of POSIX thread from a book:

    [snip]
    > It creates 10 threads. My questions are:
    > 1. The 10 threads share the same code. Do the 10 threads share both
    > main() and myThread(), or just myThread()?


    Everything, though if myThread() exits, it will not return to main()
    but terminate the thread. myThread _can_ for example call main() if
    it likes, though...

    > 2. The 10 threads share the same variables. Do the 10 threads share all
    > the variables in the code? or only global variables?


    Depends on what you mean by "share". They share all the process memory,
    so they will be _able_ to access all variables (which I would hope would
    most often be a bug), though the symbols used for stack-allocated
    variables will refer to the thread's own copy in the running code.

    > 3. The variable protVariable (LINE1) is shared among the 10 threads.
    > All the 10 threads can modify protVariable, right?


    Yes. Hence the need to protect the variable use with mutices, otherwise
    you might end up with inconsistent results.

    > 4. I use the following command to compile it:
    > gcc -o pt pthread.c


    You need the -pthread switch to compile threaded code with gcc.

    --
    Mikko Rauhala - mjr@iki.fi -
    Transhumanist - WTA member -
    Singularitarian - SIAI supporter -


  3. Re: POSIX thread question

    On Mon, 28 Aug 2006 22:41:28 +0000, Mikko Rauhala wrote:

    >
    > You need the -pthread switch to compile threaded code with gcc.

    Shouldn't be -lpthread?
    --
    Hilsen/Regards
    Michael Rasmussen
    http://keyserver.veridis.com:11371/p...rch=0xE3E80917


  4. Re: POSIX thread question


    Mikko Rauhala wrote:
    > On 28 Aug 2006 14:15:21 -0700, Jack wrote:
    > > Below is simple code of POSIX thread from a book:

    > [snip]
    > > It creates 10 threads. My questions are:
    > > 1. The 10 threads share the same code. Do the 10 threads share both
    > > main() and myThread(), or just myThread()?

    >
    > Everything, though if myThread() exits, it will not return to main()
    > but terminate the thread. myThread _can_ for example call main() if

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    How to do
    it?

    After the following line is executed:
    ret = pthread_create( &threadIds[i], NULL, myThread, NULL );

    The ith thread is created, then the ith thread enters myThread()
    function and remains there, right?

    The following line is in main() function:
    ret = pthread_mutex_destroy( &cntr_mutex );

    Only the 10 threads' parent process can execute the above line and
    destroy the cntr_mutex. The 10 threads can not destroy the cntr_mutex
    since they do not execute main() function, right?

    Thanks.

    Jack


  5. Re: POSIX thread question


    Michael Rasmussen wrote:
    > On Mon, 28 Aug 2006 22:41:28 +0000, Mikko Rauhala wrote:
    >
    > >
    > > You need the -pthread switch to compile threaded code with gcc.

    > Shouldn't be -lpthread?


    Only if '-pthread' doesn't work for some reason. The '-pthread' option
    is standard. The '-lpthread' option happens to be correct for some
    current Linux platforms.

    DS


  6. Re: POSIX thread question


    Jack wrote:

    > Only the 10 threads' parent process can execute the above line and
    > destroy the cntr_mutex. The 10 threads can not destroy the cntr_mutex
    > since they do not execute main() function, right?


    In general, a "do not" statement can't justify a "can not" statement.
    It would be an error say I "can not" eat a pickle because I "do not"
    have any pickles. I can go upstairs to my kitchen and get a pickle.

    You are correct that your threads do not execute main and hence do not
    destroy the mutex. But they could, if they were coded to do so
    (intentionally or by accident).

    DS


  7. Re: POSIX thread question


    David Schwartz wrote:
    > Michael Rasmussen wrote:


    >
    > Only if '-pthread' doesn't work for some reason. The '-pthread' option
    > is standard. The '-lpthread' option happens to be correct for some
    > current Linux platforms.
    >

    I tried the following command, it works:
    gcc -pthread -o threadapp threadapp.c

    My book suggests the following:
    gcc -pthread threadapp.c -o threadapp -lpthread

    What is the different? Which on is better?

    Thanks.

    Jack


  8. Re: POSIX thread question

    On 29 Aug 2006 08:17:52 -0700, Jack wrote:
    > I tried the following command, it works:
    > gcc -pthread -o threadapp threadapp.c
    >
    > My book suggests the following:
    > gcc -pthread threadapp.c -o threadapp -lpthread
    >
    > What is the different? Which on is better?


    There is really no difference. -pthread implies -lpthread
    (on GNU/Linux, anyway), so the latter version is just redundant.

    --
    Mikko Rauhala - mjr@iki.fi -
    Transhumanist - WTA member -
    Singularitarian - SIAI supporter -


  9. Re: POSIX thread question


    Jack wrote:

    > I tried the following command, it works:
    > gcc -pthread -o threadapp threadapp.c


    > My book suggests the following:
    > gcc -pthread threadapp.c -o threadapp -lpthread


    > What is the different? Which on is better?


    The first is better. The first will work whether or not there happens
    to be a library called 'libpthread' that is needed for POSIX pthreads.
    The second may fail or do the wrong thing if such a library does not
    exist, is not needed, or does the wrong thing.

    DS


+ Reply to Thread