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 8433859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:10+00:00 2026-06-10T06:30:10+00:00

Is there any way to use the Interlocked.CompareExchange(); and Interlocked.Increment(); methods against values stored

  • 0

Is there any way to use the Interlocked.CompareExchange(); and Interlocked.Increment(); methods against values stored in a memory-mapped file?

I’d like to implement a multi-threaded service that will store its data in a memory-mapped file, but since it’s multi-threaded I need to prevent conflicting writes, therefore I wonder about the Interlocked operations rather than using explicit locks.

I know it’s possible with native code, but can it be done in managed code on .NET 4.0?

  • 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-06-10T06:30:11+00:00Added an answer on June 10, 2026 at 6:30 am

    OK, this is how you do it! We had to figure this out, and I figured we could give some back to stackoverflow!

    class Program
    {
    
        internal static class Win32Stuff
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            unsafe public static extern int InterlockedIncrement(int* lpAddend);
        }
    
        private static MemoryMappedFile _mmf;
        private static MemoryMappedViewStream _mmvs;
    
        unsafe static void Main(string[] args)
        {
            const int INT_OFFSET = 8;
    
            _mmf = MemoryMappedFile.CreateOrOpen("SomeName", 1024);
    
            // start at offset 8 (just for example)
            _mmvs = _mmf.CreateViewStream(INT_OFFSET, 4); 
    
            // Gets the pointer to the MMF - we dont have to worry about it moving because its in shared memory
            var ptr = _mmvs.SafeMemoryMappedViewHandle.DangerousGetHandle(); 
    
            // Its important to add the increment, because even though the view says it starts at an offset of 8, we found its actually the entire memory mapped file
            var result = Win32Stuff.InterlockedIncrement((int*)(ptr + INT_OFFSET)); 
        }
    }
    

    This does work, and works across multiple processes! Always enjoy a good challenge!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to use Zend_Filter_Encrypt with large files, without rising memory limit
Is there any way to use regex match on a stream in python? like
Is there any way to use Dropbox to sync selected local files? I like
Is there any way to use Dapper.NET with stored procs that return multiple result
Is there any way to use boolean algebra in JS? Eg I would like
Is there any way to use typed variables in javascript? Like integer, string, float...
i download a KML file..Is there any way to use javascript/php to loop through
Is there any way to use <input type=file> in Safari on iPhone/iPod Touch? Or
Is there any way to use a php file in the parent folder. I
Is there any way to use environment variables in the play! application.conf file? Something

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.