Exemplo n.º 1
0
        public void LockTasksWithManyVariables()
        {
            int countTask      = 5;
            int countVariables = 10;

            LockRef <int>[] variable = new LockRef <int> [countVariables];

            for (int i = 0; i < variable.Length; ++i)
            {
                variable[i] = new LockRef <int>(i);
            }
            int[] randomNumbers = GenerateRandomNumbers(countTask, countVariables);

            List <Task> taskList = new List <Task>();

            for (int i = 0; i < countTask; ++i)
            {
                LockRef <int> tempRef = variable[randomNumbers[i]];
                taskList.Add(
                    Task.Run(() =>
                {
                    LockManager.Do(() =>
                    {
                        int temp = tempRef.Get();
                        temp    += (10 * i);
                        tempRef.Set(temp);
                    });
                })
                    );
            }

            Task.WaitAll(taskList.ToArray());
        }
Exemplo n.º 2
0
        public static int StartTasksWithLock()
        {
            LockRef <int> variable = new LockRef <int>(0);

            List <Task> taskList = new List <Task>();

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    variable.Set(2);
                    int temp = variable.Get();
                    temp    += 3;
                    variable.Set(temp);
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    variable.Set(3);
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    variable.Set(4);
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int temp = variable.Get();
                    temp    += 9;
                    variable.Set(temp);
                });
            }
                         )
                );

            Task.WaitAll(taskList.ToArray());

            return(variable.Get());
        }
Exemplo n.º 3
0
        //[Benchmark(Description = "ReadingTasksWithLock")]
        public int StartReadingTasksWithLock()
        {
            LockRef <int> variable = new LockRef <int>(0);
            List <Task>   taskList = new List <Task>();

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int[] temp = new int[10];
                    for (int i = 0; i < 10; i++)
                    {
                        temp[i] = variable.Get();
                    }
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int[] temp = new int[10];
                    for (int i = 0; i < 10; i++)
                    {
                        temp[i] = variable.Get();
                    }
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int[] temp = new int[10];
                    for (int i = 0; i < 10; i++)
                    {
                        temp[i] = variable.Get();
                    }
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int[] temp = new int[10];
                    for (int i = 0; i < 10; i++)
                    {
                        temp[i] = variable.Get();
                    }
                });
            }
                         )
                );

            taskList.Add(
                Task.Run(() =>
            {
                LockManager.Do(() =>
                {
                    int[] temp = new int[10];
                    for (int i = 0; i < 10; i++)
                    {
                        temp[i] = variable.Get();
                    }
                });
            }
                         )
                );

            Task.WaitAll(taskList.ToArray());
            return(variable.Get());
        }