コード例 #1
0
ファイル: A7Synchronization.cs プロジェクト: shangshen/cozy
        static void SyncSample()
        {
            object obj       = new object();
            bool   lockTaken = false;

            Monitor.TryEnter(obj, 500, ref lockTaken);
            if (lockTaken)
            {
                try
                {
                    // acquired the lock
                    // synchronized region for obj
                }
                finally
                {
                    Monitor.Exit(obj);
                }
            }
            else
            {
                // didn't get the lock, do something else
            }

            int numTasks = 20;
            var state    = new SharedState();
            var tasks    = new Task[numTasks];

            for (int i = 0; i < numTasks; i++)
            {
                tasks[i] = Task.Run(() => new Job(state).DoTheJob());
            }

            for (int i = 0; i < numTasks; i++)
            {
                tasks[i].Wait();
            }

            Console.WriteLine("summarized {0}", state.State);
        }
コード例 #2
0
ファイル: A7Synchronization.cs プロジェクト: xxy1991/cozy
        static void SyncSample()
        {
            object obj = new object();
            bool lockTaken = false;
            Monitor.TryEnter(obj, 500, ref lockTaken);
            if (lockTaken)
            {
                try
                {
                    // acquired the lock
                    // synchronized region for obj
                }
                finally
                {
                    Monitor.Exit(obj);
                }

            }
            else
            {
                // didn't get the lock, do something else
            }

            int numTasks = 20;
            var state = new SharedState();
            var tasks = new Task[numTasks];

            for (int i = 0; i < numTasks; i++)
            {
                tasks[i] = Task.Run(() => new Job(state).DoTheJob());
            }

            for (int i = 0; i < numTasks; i++)
            {
                tasks[i].Wait();
            }

            Console.WriteLine("summarized {0}", state.State);
        }
コード例 #3
0
ファイル: Job.cs プロジェクト: xxy1991/cozy
 public Job(SharedState sharedState)
 {
     this.sharedState = sharedState;
 }
コード例 #4
0
 public Job(SharedState sharedState)
 {
     this.sharedState = sharedState;
 }