Exemplo n.º 1
0
        private static void QueueTest()
        {
            Console.WriteLine("QueueTest:");
            var buffer = new STM.Collections.Queue <int>();
            var t1     = new Thread(() =>
            {
                var sw = Stopwatch.StartNew();
                for (var i = 0; i < 1000; i++)
                {
                    Console.WriteLine(buffer.Dequeue());
                    Console.WriteLine("Index :" + i);
                }

                sw.Stop();
                Console.WriteLine("Milisecs: " + sw.ElapsedMilliseconds);
            });

            var t2 = new Thread(() =>
            {
                for (var i = 0; i < 1000; i++)
                {
                    buffer.Enqueue(i);
                }
            });

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();
        }
Exemplo n.º 2
0
        public static void RetryExample(STM.Collections.Queue <Person> buffer)
        {
            Person result = STMSystem.Atomic(() =>
            {
                if (buffer.Count == 0)
                {
                    STMSystem.Retry();   //Initiates retry
                }
                return(buffer.Dequeue());
            });

            //Work with the result

            STMSystem.Retry(); //Has no effect
        }
Exemplo n.º 3
0
        public static void NestingExample()
        {
            TMVar <string> s      = new TMVar <string>(string.Empty);
            var            result = STMSystem.Atomic(() =>
            {
                s.Value = "abc";
                STMSystem.Atomic(() =>
                {
                    s.Value = s + "def";
                });

                return(s.Value);
            });

            STM.Collections.Queue <Person> buffer1 = new STM.Collections.Queue <Person>();
            STM.Collections.Queue <Person> buffer2 = new STM.Collections.Queue <Person>();

            STMSystem.Atomic(() =>
            {
                var item = buffer1.Dequeue();
                buffer2.Enqueue(item);
            });
        }