home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11749 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  1.4 KB

  1. Path: sparky!uunet!darwin.sura.net!wupost!think.com!barmar
  2. From: barmar@think.com (Barry Margolin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to avoid calling destructor twice after fork()?
  5. Date: 29 Jul 1992 20:41:59 GMT
  6. Organization: Thinking Machines Corporation, Cambridge MA, USA
  7. Lines: 24
  8. Message-ID: <156vqnINNj3f@early-bird.think.com>
  9. References: <1992Jul29.180655.4716@cis.ohio-state.edu>
  10. NNTP-Posting-Host: telecaster.think.com
  11.  
  12. In article <1992Jul29.180655.4716@cis.ohio-state.edu> gyu@cis.ohio-state.edu (george yu) writes:
  13. >I am using fork() system call in my C++ program. The problem is that each
  14. >calling of fork() creates a copy of calling process. The problem is any
  15. >instances created and not destroyed before fork() calling will be 
  16. >destroyed twice.
  17.  
  18. For most purposes, this should be OK.  Each process has its own copy of the
  19. object, so if the destructor is used to deallocate space it's necessary to
  20. do so in each process.
  21.  
  22. But if the destructor communicates with external entities, I can see how
  23. this would be incorrect.
  24.  
  25. I don't think you can prevent the destructor from being called.  However,
  26. you can have the destructor skip the parts that shouldn't be done twice.
  27. After forking, you can set a member in the object that indicates whether
  28. it's the parent or child's copy, and the destructor can look at this member
  29. to decide how to behave.
  30.  
  31. -- 
  32. Barry Margolin
  33. System Manager, Thinking Machines Corp.
  34.  
  35. barmar@think.com          {uunet,harvard}!think!barmar
  36.