当前位置:编程学习 > 网站相关 >>

化零为整WCF(13) - 并发控制(锁)(Mutex, Semaphore, Monitor, Lock,&

作者:webabcd


介绍
WCF(Windows Communication Foundation) - 并发控制:以ConcurrencyMode.Multiple并发模式及InstanceContextMode.Single实例模型为例(此时有并发问题),介绍如何做并发控制,即各种锁的使用(Mutex, Semaphore, Monitor, Lock, ThreadPool, Interlocked, ReaderWriterLock)


示例
1、服务
Enum.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WCF.ServiceLib.ConcurrencyLock
{
    /**//// <summary>
    /// 锁 类型的枚举
    /// </summary>
    [DataContract]
    public enum LockType
    {
        /**//// <summary>
        /// 不使用任何并发控制
        /// </summary>
        [EnumMember]
        None,
        /**//// <summary>
        /// Mutex
        /// </summary>
        [EnumMember]
        Mutex,
        /**//// <summary>
        /// Semaphore
        /// </summary>
        [EnumMember]
        Semaphore,
        /**//// <summary>
        /// Monitor
        /// </summary>
        [EnumMember]
        Monitor,
        /**//// <summary>
        /// Lock
        /// </summary>
        [EnumMember]
        Lock
    }
}

IHello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.ConcurrencyLock
{
    /**//// <summary>
    /// 演示并发控制(锁)的接口
    /// </summary>
    [ServiceContract]
    public inte易做图ce IHello
    {
        /**//// <summary>
        /// 计数器
        /// </summary>
        /// <param name="lockType">锁的类型</param>
        [OperationContract]
        void Counter(LockType lockType);

        /**//// <summary>
        /// 获取计数器被调用的结果
        /// </summary>
        /// <returns></returns>
        [OperationContract]
        string GetResult();

        /**//// <summary>
        /// 清空计数器和结果
        /// </summary>
        [OperationContract]
        void CleanResult();
    }
}

Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;

namespace WCF.ServiceLib.ConcurrencyLock
{
    /**//// <summary>
    /// 演示并发控制(锁)的接口
    /// </summary>
    /// <remarks>
    /// ServiceBehavior - 指定服务协定实现的内部执行行为
    /// 实例模型:单例;并发模式:多线程
    /// 会有并发问题,通过 锁 来解决
    /// </remarks>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Hello : IHello
    {
        private int _counter;
        private string _result;

        private System.Threading.Mutex _mutex = new System.Threading.Mutex();

        // 此构造函数初始化未命名的信号量。所有使用这类信号量的实例的线程都必须具有对该实例的引用。
        // 如果 initialCount 小于 maximumCount,则效果与当前线程调用了 WaitOne(maximumCount 减去 initialCount)次相同。如果不想为创建信号量的线程保留任何入口,请对 maximumCount 和 initialCount 使用相同的数值。
        private System.Threading.Semaphore _semaphore = new System.Threading.Semaphore(1, 1);

        private static readonly object objLock = new object();


        /**//// <summary>
        /// 计数器
        /// </summary>
        /// <returns></returns>
        public void Counter(LockType lockType)
        {
            switch (lockType)
            {
                case LockType.None:
                    ExecuteNone();
                    break;
                case LockType.Mutex:
                    ExecuteMutex();
                    break;
                case LockType.Semaphore:
                    ExecuteSemaphore();
                    break;
                case LockType.Monitor:
                    ExecuteMonitor();
                    break;
                case LockType.Lock:
    &n

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,