Data sharing in POSIX thread? - Linux

This is a discussion on Data sharing in POSIX thread? - Linux ; How is data shared in POSIX thread? Below is a sample code: #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 = ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Data sharing in POSIX thread?

  1. Data sharing in POSIX thread?

    How is data shared in POSIX thread? Below is a sample code:

    #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 );

    }

    char *p;
    p = malloc(1000); //LINE2

    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;

    }

    My book says all the threads of the same process share the same data.
    In the above code, 10 threads are created. Do they share all the
    variables? For the global variable, protVariable, the 10 threads should
    share it, it is understandable. How about the local variables i and ret
    in
    myThread()? Does each thread have its own copy of local variables or do
    all the threads share the local variables? If I allocate memory in
    myThread() using malloc as LINE2, do all the threads share the same
    allocated memory? I can not find the answers from my book.

    Thanks.

    Jack


  2. Re: Data sharing in POSIX thread?


    Jack wrote:

    > My book says all the threads of the same process share the same data.
    > In the above code, 10 threads are created. Do they share all the
    > variables? For the global variable, protVariable, the 10 threads should
    > share it, it is understandable. How about the local variables i and ret
    > in
    > myThread()? Does each thread have its own copy of local variables or do
    > all the threads share the local variables?


    Both. Each thread has its own copy of the local variables and each of
    those are shared.

    For example, if thread 1 and thread 2 each print out the address of the
    "same" local variable, they will print different addresses. If they
    somehow communicate those addresses to each other, each can access the
    other's variable (assuming they are careful to do so during its
    lifetime!).

    > If I allocate memory in
    > myThread() using malloc as LINE2, do all the threads share the same
    > allocated memory? I can not find the answers from my book.


    If each thread calls 'malloc', then each thread allocates memory. Those
    blocks of memory are fully shared and nothing stops one thread from
    accessing the memory another thread allocated.

    This is a *very* important concept to understand. Questions like "does
    each thread have its own stack or are the stacks shared" try to
    pigeon-hole into two possibilities and ignore the third possibility --
    each thread does have its own stack (that is, there is one stack per
    thread) but they are also shared (that is, each thread can access
    another thread's stack should it be coded to do so or do so through a
    bug).

    DS


  3. Re: Data sharing in POSIX thread?


    David Schwartz wrote:
    > Jack wrote:
    >
    > > My book says all the threads of the same process share the same data.
    > > In the above code, 10 threads are created. Do they share all the
    > > variables? For the global variable, protVariable, the 10 threads should
    > > share it, it is understandable. How about the local variables i and ret
    > > in
    > > myThread()? Does each thread have its own copy of local variables or do
    > > all the threads share the local variables?

    >
    > Both. Each thread has its own copy of the local variables and each of
    > those are shared.
    >
    > For example, if thread 1 and thread 2 each print out the address of the
    > "same" local variable, they will print different addresses. If they
    > somehow communicate those addresses to each other, each can access the
    > other's variable (assuming they are careful to do so during its
    > lifetime!).
    >
    > > If I allocate memory in
    > > myThread() using malloc as LINE2, do all the threads share the same
    > > allocated memory? I can not find the answers from my book.

    >
    > If each thread calls 'malloc', then each thread allocates memory. Those
    > blocks of memory are fully shared and nothing stops one thread from
    > accessing the memory another thread allocated.

    Does it means that if a thread allocate memory the thread must protect
    the allocated memory. Otherwise all threads can access it and the
    result is unpredictable.

    >
    > This is a *very* important concept to understand. Questions like "does
    > each thread have its own stack or are the stacks shared" try to
    > pigeon-hole into two possibilities and ignore the third possibility --
    > each thread does have its own stack (that is, there is one stack per
    > thread) but they are also shared (that is, each thread can access
    > another thread's stack should it be coded to do so or do so through a
    > bug).

    How to tell whether a stack is shared or not?

    Thanks.


  4. Re: Data sharing in POSIX thread?


    Jack wrote:

    > Does it means that if a thread allocate memory the thread must protect
    > the allocated memory. Otherwise all threads can access it and the
    > result is unpredictable.


    No. Threads are not at war with each other. They're more like members
    of a well-functioning household. You don't have to lock your bedroom
    door because nobody is going to steal your sheets. If someone is, the
    fix is to make them stop, not to lock the door.

    No, if we *want* another thread to be able to access that memory
    safely, we may need to use a synchronization mechanism like a lock. But
    that's much like the lock on the bathroom door. It's not to keep evil
    people out, it's to let good people know that they must wait to use the
    bathroom if they want to keep the efficient cooperation working.

    > > This is a *very* important concept to understand. Questions like "does
    > > each thread have its own stack or are the stacks shared" try to
    > > pigeon-hole into two possibilities and ignore the third possibility --
    > > each thread does have its own stack (that is, there is one stack per
    > > thread) but they are also shared (that is, each thread can access
    > > another thread's stack should it be coded to do so or do so through a
    > > bug).


    > How to tell whether a stack is shared or not?


    It's not clear precisely what you're asking. Stacks are always shared
    in the sense that one thread can access another thread's stack if it is
    coded to do so (or somehow does so erroneously due to a bug). They are
    also never shared in the sense that each thread has its own stack to
    work from. This is always the case with POSIX threading, so there is
    nothing to check on. (Although theoretically threads could trade stacks
    or you could have more stacks than threads and 'pick up' a new stack,
    this is rarely done and only when you know exactly what you're doing
    and why.)

    DS


+ Reply to Thread