?
Solved

Multithreading and Memory on HP-UX (Posix Threads)

Posted on 2004-10-22
3
Medium Priority
?
358 Views
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_init(&thAttrs);
      pthread_attr_setdetachstate(&thAttrs, PTHREAD_CREATE_DETACHED);
      int ret = pthread_create(&tid, &thAttrs, MyFunc, (void *)myPtr);
      pthread_attr_destroy(&thAttrs);

If I run the exact code single threaded, the crash disappears.  Any help is appreciated.
0
Comment
Question by:mromeo
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 1000 total points
ID: 12386104
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.

http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html
http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/multi-thread.html
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
0
 

Author Comment

by:mromeo
ID: 12400065
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.
0
 

Author Comment

by:mromeo
ID: 12400075
And like I said earlier, the exact code works great on both Linux and SunOS.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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