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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:56:55+00:00 2026-05-27T19:56:55+00:00

I’m working on a lock free stack and queue data stracture where I can

  • 0

I’m working on a lock free stack and queue data stracture where I can place as many add items as I want and collect all the items in a single call, I think my design is solid and it is working as expected until I started to recive an unexpected exception which I thought is impossible in a pure C# envirement:

Managed Debugging Assistant ‘FatalExecutionEngineError’ has detected a problem Additional Information: The runtime has encountered a fatal error. The address of the error was at 0x6caf6ac7, on thread 0x16f0. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

I cant seem to be able to find way is it happening and would like to know if someone can direct me to the cause of the error:

This is the implemention of the stackbulkcollector:

public class StackBulkCollector<T> : IBulkCollector<T>
{        
    class Node
    {
        public T value;
        private bool m_isSet;
        private Node m_prev;

        public Node(T data)
        {
            value = data;
        }

        public Node()
        {                
        }

        public Node Prev
        {
            set
            {
                m_prev = value;
                m_isSet = true;
            }

            get
            {
                if (!m_isSet)
                {
                    SpinWait s = new SpinWait();
                    while (!m_isSet)
                    {
                        s.SpinOnce();
                    }
                }

                return m_prev;
            }
        }
    }

    class Enumerable : IEnumerable<T>
    {
        private Node m_last;

        public Enumerable(Node last)
        {
            m_last = last;
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new Enumerator(m_last);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new Enumerator(m_last);
        }
    }

    class Enumerator : IEnumerator<T>
    {
        private readonly Node m_last;
        private Node m_current;

        public Enumerator(Node last)
        {
            var node = new Node();
            m_current = m_last = node;
            node.Prev = last;
        }

        public T Current
        {
            get { return m_current.value; }
        }

        public void Dispose()
        {
        }

        object System.Collections.IEnumerator.Current
        {
            get { return this; }
        }

        public bool MoveNext()
        {
            if (m_current == null)
            {
                return false;
            }

            m_current = m_current.Prev;
            return m_current != null;
        }

        public void Reset()
        {
            m_current = m_last;
        }
    }

    private Node m_last;

    public void Add(T data)
    {
        var node = new Node(data);
        node.Prev = Interlocked.Exchange(ref m_last, node);
    }

    public IEnumerable<T> GetBulk()
    {
        var last = Interlocked.Exchange(ref m_last, null);
        return new Enumerable(last);
    }
}

And this is the tester program I use to test it:

class Program
{
    public static readonly int UseThreads = 4;
    public static readonly TimeSpan Duration = new TimeSpan(0,0,0,3);

    public static long[] AddedItems = new long[UseThreads];

    static void Main(string[] args)
    {
        IBulkCollector<TestData> bulkCollector = new QueuedBulkCollector<TestData>();
        while (true)
        {
            using (var countdownEvent = new CountdownEvent(UseThreads + 1))
            {
                var results = new Dictionary<int, List<TestData>>();
                if (bulkCollector is QueuedBulkCollector<TestData>)
                {
                    bulkCollector = new StackBulkCollector<TestData>();
                    Console.WriteLine("Starting StackBulkCollector Test");
                }
                else
                {
                    bulkCollector = new StackBulkCollector<TestData>();
                    Console.WriteLine("Starting QueuedBulkCollector Test");
                }
                for (int i = 0; i < UseThreads; i++)
                {
                    results[i] = new List<TestData>();
                    Thread t = new Thread(PushTestData);
                    t.Start(new object[] {i, bulkCollector, countdownEvent});
                }

                var start = DateTime.Now;
                Thread readerThred = new Thread(() =>
                                                    {
                                                        while ((DateTime.Now - start) <
                                                               (Duration - new TimeSpan(0, 0, 0, 0, 100)))
                                                        {
                                                            foreach (var testData in bulkCollector.GetBulk())
                                                            {
                                                                results[testData.Id].Add(testData);
                                                            }
                                                            //Console.WriteLine("Doing Some Read {0}", currBulk.Count);
                                                            System.Threading.Thread.Sleep(10);
                                                        }
                                                        countdownEvent.Signal();
                                                    });
                readerThred.Start();
                countdownEvent.Wait();

                var lastBulk = bulkCollector.GetBulk().ToList();
                foreach (var testData in lastBulk)
                {
                    results[testData.Id].Add(testData);
                }

                Console.WriteLine("Doing Last Read {0}", lastBulk.Count);

                long[] value = new long[UseThreads];
                long totalCount = 0;
                int errorCount = 0;
                for (int i = 0; i < UseThreads; i++)
                {
                    value[i] = AddedItems[i];
                    totalCount += value[i];
                    Console.WriteLine("Thread {0} Push {1} Items", i, value[i]);
                    var verifyArray = results[i].OrderBy(p => p.Value).ToList();
                    if (verifyArray.Count != value[i])
                    {
                        Console.WriteLine("Not Working Count miss match");
                        errorCount++;
                    }
                    else
                    {
                        var expected = 0;
                        foreach (var testData in verifyArray)
                        {
                            if (expected != testData.Value)
                            {
                                Console.WriteLine("NotWorking");
                                errorCount++;
                            }
                            expected++;
                        }
                    }
                }
                Console.WriteLine("Done Total Push {0} with {1} errors.", totalCount.ToString("#,##0") , errorCount);
                if (errorCount != 0)
                {
                    Console.ReadKey();
                }
            }
        }
    }

    private static void PushTestData(object o)
    {
        object[] parms = o as object[];
        int id = (int)parms[0];
        IBulkCollector<TestData> bulkCollector = (IBulkCollector<TestData>)parms[1];
        CountdownEvent endEvetn = (CountdownEvent)parms[2];
        AddedItems[id] = 0;
        var start = DateTime.Now;
        while ((DateTime.Now - start) < Duration)
        {
            bulkCollector.Add(new TestData() { Id = id, Value = AddedItems[id]});
            AddedItems[id]++;
        }

        endEvetn.Signal();
    }
}

Any advice will be most welcome.

  • 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-27T19:56:56+00:00Added an answer on May 27, 2026 at 7:56 pm

    Sorry, A defective memory module was the cause of the problem, it has been replaced and the problem was solved.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.