Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3950668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:38:20+00:00 2026-05-20T01:38:20+00:00

As DateTime cannot be declared as volatile , is this right? private DateTime _time;

  • 0

As DateTime cannot be declared as volatile, is this right?

private DateTime _time;
public DateTime Time
{
    get
    {
        Thread.MemoryBarrier();
        return _time;
    }
    set
    {
        _time = value;
        Thread.MemoryBarrier();
    }
}

That property could be accessed from different threads, so I want to ensure they get always the latest version, without use contention (lock).

EDIT:

  • I have a collection of hard-to-create items, each one has a DateTime property named CreationTime, indicating when this item was created. It’s initialized to DateTime.UtcNow.
  • Every time a item is accessed, that property is updated to DateTime.UtcNow.
  • There is a thread, that executes in timely fashion in a threaded timer that checks if (DateTime.UtcNow + 1 hour) > item.CreationTime, if true it deletes the item.

I want to ensure that when the "deletion thread" comes into the collection, all the items have their latest "last access" DateTime on it, so I can avoid create the item again just because a cache held the value for a couple of milliseconds. 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:38:20+00:00Added an answer on May 20, 2026 at 1:38 am

    Precisely.

    But, you have another option. Store the time as an Int64 tick count, and use InterlockedExchange to set. Threads can then construct their own DateTime using The Int64 constructor, giving you no contention and no locks.

    EDIT:

    Given that you’ve provided more information, it’s easier now to provide an example.

    public class Cache
    {
        class CacheEntry
        {
            private Int64 m_Touched;
    
            public CacheEntry()
            {
                Touch();
            }
    
            public void Touch() 
            {
                System.Threading.Interlocked.Exchange(ref m_Touched, DateTime.Now.Ticks);
            }
    
            public DateTime Touched
            {
                get
                {
                    return new DateTime(Interlocked.Read(ref m_Touched));
                }
            }
        } // eo class CacheEntry
    } // eo class Cache
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of my columns type is DateTime (Date Registered). I cannot create a query
Given a specific DateTime value, how do I display relative time, like: 2 hours
Given a datetime.time value in Python, is there a standard way to add an
If I have a datetime field, how do I get just records created later
Given a DateTime representing a person's birthday, how do I calculate their age in
I have two DateTime objects: StartDate and EndDate . I want to make sure
Besides the DateTime ctor (new DateTime(year, month, day)) , is there any other way
I've got a DateTime? that I'm trying to insert into a field using a
For example: from datetime import <c-x><c-o>{list of modules inside datetime package}
I have a Ruby DateTime which gets filled from a form. Additionally I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.