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;