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

  • Home
  • SEARCH
  • 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 721507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:52:53+00:00 2026-05-14T05:52:53+00:00

I need to implement a FIFO queue for messages on a game server so

  • 0

I need to implement a FIFO queue for messages on a game server so it needs to as fast as possible. There will be a queue for each user.

The queue will have a maxiumem size (lets say 2000). The size won’t change during runtime.

I need to prioritize messages ONLY if the queue reaches its maximum size by working backwards and removing a lower priority message (if one exists) before adding the new message.

A priority is an int with possible values of 1, 3, 5, 7, 10.

There can be multiple messages with the same priority.

A message cannot change its priority once allocated.

The application is asynchronous so access to the queue needs to be locked.

I’m currently implementing it using a LinkedList as the underlying storage but have concerns that searching and removing nodes will keep it locked for too long.

Heres the basic code I have at the moment:

public class ActionQueue
{
    private LinkedList<ClientAction> _actions = new LinkedList<ClientAction>();
    private int _maxSize;

    /// <summary>
    /// Initializes a new instance of the ActionQueue class.
    /// </summary>
    public ActionQueue(int maxSize)
    {
        _maxSize = maxSize;
    }

    public int Count
    {
        get { return _actions.Count; }            
    }

    public void Enqueue(ClientAction action)
    {
        lock (_actions)
        {
            if (Count < _maxSize)
                _actions.AddLast(action);
            else
            {
                LinkedListNode<ClientAction> node = _actions.Last;
                while (node != null)
                {
                    if (node.Value.Priority < action.Priority)
                    {
                        _actions.Remove(node);
                        _actions.AddLast(action);
                        break;
                    }

                    node = node.Previous;

                }                    
            }
        }
    }

    public ClientAction Dequeue()
    {
        ClientAction action = null;

        lock (_actions)
        {
            action = _actions.First.Value;
            _actions.RemoveFirst();
        }

        return action;
    }

}
  • 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-14T05:52:53+00:00Added an answer on May 14, 2026 at 5:52 am

    So we have the following properties:

    • Priorities are well-defined and bounded
    • Needs to be thread-safe
    • Queue size is fixed to 2000 messages, where enqueues beyond this drop the lowest item

    Its very easy to write a priority queue which supports all of these properties:

    public class BoundedPriorityQueue<T>
    {
        private object locker;
        private int maxSize;
        private int count;
        private LinkedList<T>[] Buckets;
    
        public BoundedPriorityQueue(int buckets, int maxSize)
        {
            this.locker = new object();
            this.maxSize = maxSize;
            this.count = 0;
            this.Buckets = new LinkedList<T>[buckets];
            for (int i = 0; i < Buckets.Length; i++)
            {
                this.Buckets[i] = new LinkedList<T>();
            }
        }
    
        public bool TryUnsafeEnqueue(T item, int priority)
        {
            if (priority < 0 || priority >= Buckets.Length)
                throw new IndexOutOfRangeException("priority");
    
            Buckets[priority].AddLast(item);
            count++;
    
            if (count > maxSize)
            {
                UnsafeDiscardLowestItem();
                Debug.Assert(count <= maxSize, "Collection Count should be less than or equal to MaxSize");
            }
    
            return true; // always succeeds
        }
    
        public bool TryUnsafeDequeue(out T res)
        {
            LinkedList<T> bucket = Buckets.FirstOrDefault(x => x.Count > 0);
            if (bucket != null)
            {
                res = bucket.First.Value;
                bucket.RemoveFirst();
                count--;
                return true; // found item, succeeds
            }
            res = default(T);
            return false; // didn't find an item, fail
        }
    
        private void UnsafeDiscardLowestItem()
        {
            LinkedList<T> bucket = Buckets.Reverse().FirstOrDefault(x => x.Count > 0);
            if (bucket != null)
            {
                bucket.RemoveLast();
                count--;
            }
        }
    
        public bool TryEnqueue(T item, int priority)
        {
            lock (locker)
            {
                return TryUnsafeEnqueue(item, priority);
            }
        }
    
        public bool TryDequeue(out T res)
        {
            lock (locker)
            {
                return TryUnsafeDequeue(out res);
            }
        }
    
        public int Count
        {
            get { lock (locker) { return count; } }
        }
    
        public int MaxSize
        {
            get { return maxSize; }
        }
    
        public object SyncRoot
        {
            get { return locker; }
        }
    }
    

    Supports Enqueue/Dequeue in O(1) time, the TryEnqueue and TryDequeue methods are guaranteed to be thread-safe, and the size of the collection will never exceed the max size you specify in the constructor.

    The locks on TryEnqueue and TryDequeue are pretty fine-grained, so you might take a performance hit whenever you need to bulk-load or unload data. If you need to load the queue with a lot of data up front, then lock on the SyncRoot and call the unsafe methods as needed.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.