Multithreading and Memory on HP-UX (Posix Threads)

Posted on 2004-10-22
Last Modified: 2013-12-26
When a program creates detached threads (using Posix threads), does the new thread inherit the memory space of the parent?  I am creating the threads detached, but when I try to use some memory that was created by the parent thread, the program crashes with a memory error.  Do processes need to be created not detached (attached) in order to use the parent's memory space?  I am compiling this C++ code with gcc 3.4.2 on an HP-UX 11.00 32 bit machine.  The exact code works on both Linux and Solaris, so I'm wondering if this is an HPUX problem or something that I just got away with on the other 2 OS's.   Here is how I"m creating the threads:

      pthread_t tid;
      pthread_attr_t thAttrs;
      pthread_attr_setdetachstate(&thAttrs, PTHREAD_CREATE_DETACHED);
      int ret = pthread_create(&tid, &thAttrs, MyFunc, (void *)myPtr);

If I run the exact code single threaded, the crash disappears.  Any help is appreciated.
Question by:mromeo
    LVL 23

    Accepted Solution

    By definition, all threads have access to the entire address space of the process.
    However, this full access means that the threads can have 'collisions' - reading
    and/or writing the same memory location at the same time, corrupting data.
    For that reason, threads programming usually requires the use of thread coordination
    primitives such as mutexes, semaphores, condition variables, and critical sections.
    These allow threads to coordinate access to common data, signal each other,
    sleep and wake up, etc.  Consider one of these threads programming tutorial.

    Author Comment

    I am using mutexes to prevent these kinds of collisions.  In addition, my early testing only involves creating a single thread (in addition to the parent).  When I take the calls to pthread_xxx out, the program works fine.  When I put them back in, it crashes.  It seems to crash when it is in an idle state.

    Author Comment

    And like I said earlier, the exact code works great on both Linux and SunOS.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now