Home » Community » Coffee corner » Sharing and Locking
Re: Sharing and Locking [message #25949 is a reply to message #25948] |
Sun, 21 March 2010 14:21   |
 |
mirek
Messages: 14255 Registered: November 2005
|
Ultimate Member |
|
|
gridem wrote on Sun, 21 March 2010 06:39 |
luzr wrote on Sun, 21 March 2010 09:37 |
You miss the point: When the file is closed?
(I know when, of course, but the point is the shared ownership makes this very uncertain).
|
OK, usage sample:
void SetterThread()
{
for (int i = 0; i < cycles; ++ i)
{
// create file object
FileObject file;
// assign reference to global variable
*DataAccess::Access() = file;
// create file itself
file.Open("file.txt");
// write some text, file will be opened because accesser doesn't use close
// (try ... catch is not needed)
file.Write(String().Cat() << "[" << i << "] setter");
// close the file, accesser now cannot write into file
file.Close();
}
}
void AccesserThread()
{
for (int i = 0; i < cycles; ++ i)
{
try
{
// try to get the real object from global reference
FileObject file = DataAccess::Access()->Get();
for (int j = 0; j < internalCycles; ++ j)
{
// try to write into file
file.Write(String().Cat() << "[" << i << "," << j << "] accesser");
}
}
catch(Exc& e)
{
Out(String().Cat() << "[" << i << "] Accesser error: " << e);
}
}
}
In the considered implementation the File lifetime is always predictable while lifetime of FileObject can be longer.
See attached file for detailed information.
Regards,
Grigory.
|
Well, this is the exact tradeoff of GC - you have lost the capability of destructors to manage resources.
Do not get me wrong. What you present is the 'mainstream' approach. In that case, however, the question is why not to use some real GC language instead...
What we are trying to do is exactly oposite. End of block closes the file (pipe, stream, whatever). That is why shared ownership (at interface level) is not recommended...
(P.S.: Not quite sure "try/catch" is not needed there. Who will close the file if the exception leaves the block?)
Mirek
|
|
|
 |
|
Sharing and Locking
By: gridem on Sun, 07 March 2010 09:27
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 07 March 2010 14:40
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 07 March 2010 16:38
|
 |
|
Re: Sharing and Locking
By: mirek on Mon, 08 March 2010 01:42
|
 |
|
Re: Sharing and Locking
By: gridem on Mon, 08 March 2010 10:57
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 14 March 2010 13:37
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 14 March 2010 18:58
|
 |
|
Re: Sharing and Locking
By: gridem on Mon, 15 March 2010 21:26
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 06:02
|
 |
|
Re: Sharing and Locking
By: gridem on Tue, 16 March 2010 08:04
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 23:50
|
 |
|
Re: Sharing and Locking
By: gridem on Fri, 19 March 2010 07:44
|
 |
|
Re: Sharing and Locking
By: mirek on Fri, 19 March 2010 07:59
|
 |
|
Re: Sharing and Locking
By: gridem on Sat, 20 March 2010 10:21
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 21 March 2010 07:37
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 21 March 2010 11:39
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 21 March 2010 14:21
|
 |
|
Re: Sharing and Locking
By: gridem on Thu, 01 April 2010 09:14
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 06:14
|
Goto Forum:
Current Time: Sun Apr 27 17:22:40 CEST 2025
Total time taken to generate the page: 0.01352 seconds
|