I have a pool of fileserver connections. (to cache connected fileservers) Now, if there are 100 open connections in the pool, I want to cleanup after a while.
I would do a cleanup after 15 min. (e.g. close 20% of the active connections after 15 min inactivity)
For that I need to remember the timestamp of the last incomming request. (as a static variable)
static DateTime s_lastUse;
//...
UseFileServer()
{
s_lastUse = DateTime.Now;
}
//...
CreateNewConnection()
{
if((DateTime.Now - s_lastUse) > TimeSpan.FromSeconds(900))
{
//do cleanup....
}
}
Is this possible in a multithreaded architecture? Maybe, if two write-operations happen to the same time, the value in the variable is corrupt.
Do .Net ensure that this write-operation is atomic?
s_lastUse = DateTime.Now;
Is seems to be atomic: http://msdn.microsoft.com/en-us/library/aa691278%28VS.71%29.aspx
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf
Partition I, Section 12.6.6 of the CLI spec states: "A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size."