简体   繁体   中英

Locking a thread by user

I need a way to lock c# threads by user

I have my data object and I create new instance for every user. Every user has several threads that use this object and in IO operations I want to lock this object instance for this user only. Using simple Lock {} is locking all the object instances, there for blocking other user.

I need some simple solution.

Edit

I build new instance of MyDataObj per user; Then run job that updating some data in MyDataObj every minute; Using lockObj as lock, lock the data to all the users (Although it's not static Variables) I need only to lock the data to the current user

this is the code sample

public sealed class MyDataObj
{
    private static readonly Dictionary<object, MyDataObj> _instances = new Dictionary<object, MyDataObj>();
    public object lockObj = new object();
    public bool jobRunning = false;
    private string data = string.Empty;
    //// --------- constractor -------------------
    private MyDataObj(int key)
    {
        LoadMyDataObj(key);
    }

    public static MyDataObj GetInstance(int key)
    {
        lock (_instances)
        {
            MyDataObj instance;
            if (_instances.TryGetValue(key, out instance))
            {
                instance = _instances[key];
                return instance;
            }
            instance = new MyDataObj(key);
            return instance;
        }
    }

    private void LoadMyDataObj(int key)
    {
        // get the data from db

    }


    public void UpdateMyData(string newData)
    {
        lock (lockObj)
        {
            this.data = newData;
        }
    }
    public string ReadMyData()
    {
        lock (lockObj)
        {
            return this.data;
        }
    }



    public class ActionObject
    {
        MyDataObj myDataObj;
        int UserID;
        //// --------- constractor -------------------
        public ActionObject(int userid)
        {
            this.UserID = userid;
            myDataObj = MyDataObj.GetInstance(userid);
            if (!myDataObj.jobRunning)
            {
                jobs jbs = new jobs(myDataObj);
                System.Threading.Thread RunJob = new System.Threading.Thread(new System.Threading.ThreadStart(jbs.dominutesAssignment));
                RunJob.Start();
                myDataObj.jobRunning = true;
            }
        }
        public ActionObject()
        {
            myDataObj = MyDataObj.GetInstance(this.UserID);
            myDataObj.UpdateMyData("some data");
        }
    }


    public class jobs
    {
        MyDataObj myDataObj = null;

        public jobs(MyDataObj grp)
        {
            this.myDataObj = grp;
        }
        public void dominutesAssignment()
        {
            while (true)
            {
                myDataObj.ReadMyData();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

I need a way to lock c# threads by user. I have my data object and I create new instance for every user

Create one lock per user. Or if the user exists longer than the threads: Use the user object as the lock.

lock (userOrTheUserObject)
{
    //Do some op
}

Every user has several threads that use this object and in IO operations

That sounds more like you should use asynchronous IO instead of creating several threads (which will be less effecient)

I want to lock this object instance for this user only. Using simple Lock {} is locking all the object instances, there for blocking other user.

If the object is shared between all users you HAVE to lock it using lock . The lock won't be very effective otherwise. The other object is to redesign the object to now be shared.

I need some simple solution.

There are no simple threading solutions.

You can use Monitor . In this sample anyone but user 1 can execute DoIt method concurrently. While user 1 executing DoIt no one can enter it. A weak point is if user 1 tries to execute DoIt when user 2 already executing it, user 2 continues its execution. Also you have to handle exceptions properly otherwise there may be dead locks.

private static readonly object lockObj = new Object();

public void Do(int userId)
{
     Monitor.Enter(lockObj);

     if (userId != 1)
          Monitor.Exit(lockObj);

     try
     {
        DoIt();
     }
     finally 
     {
         if (userId == 1)
             Monitor.Exit(lockObj);
     }
}

public void DoIt()
{
    // Do It
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM