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