Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Coffee corner » Sharing and Locking
Re: Sharing and Locking [message #25948 is a reply to message #25939] Sun, 21 March 2010 11:39 Go to previous messageGo to previous message
gridem is currently offline  gridem
Messages: 45
Registered: August 2008
Member
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.
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Windows 7 - long wait after app crash
Next Topic: Upp application and CPU on Linux
Goto Forum:
  


Current Time: Wed May 15 02:51:42 CEST 2024

Total time taken to generate the page: 0.02698 seconds