locked
how to implement a thread safe class queue 's push and pop method using c # RRS feed

  • Question

  • User-812314942 posted

    how to implement a thread safe class queue 's  push
    and pop method,

    Thanks,

    Hong

    Thursday, July 24, 2014 2:31 PM

All replies

  • User1421620300 posted

    Try This!

    class Program
    {
    static void Main(string[] args)
    {
    List<int> myList = new List<int>();
    Queue<int> myQ = new Queue<int>();
    Stack<int> myStack = new Stack<int>();

    myStack.Push(0); //FILO
    myStack.Push(1);
    myStack.Push(2);
    myStack.Push(3);
    myStack.Push(4);
    myStack.Push(5);
    myStack.Push(6);
    myStack.Push(7);
    myStack.Push(8);
    myStack.Push(9);

    int topofStack = myStack.Pop();
    Console.WriteLine(topofStack);
    myStack.Push(8);
    Console.WriteLine("=======================");
    foreach (int i in myStack)
    {
    Console.WriteLine(i);
    }
    Console.WriteLine("=====================");

    Console.Read();

    }

    }

    }

    Friday, February 6, 2015 9:54 AM
  • User-434868552 posted

    @hong_ma

    one approach could involve using a Singleton for managing your queue.

    Implementing Singleton in C#  https://msdn.microsoft.com/en-us/library/ff650316.aspx

    another approach might be to use message queuing https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx "Message Queuing (MSMQ)"

    TIMTOWTDI

    Saturday, February 7, 2015 2:01 PM