KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to lock a "boxed" object in a c# app, is this not possible? class t { System.Object t_x = new object(); public t(int p) { t_x = p; } public void w() { lock (t_x) { for (int i = 0; i < 4; i++) { { t_x = ((int)t_x) + 1; Console.WriteLine(t_x); Thread.Sleep(1000); } } } } } In another class I can start 2 threads: Thread b1 = new Thread(new ThreadStart(t1.w)); b1.Start(); Thread b2 = new Thread(new ThreadStart(t1.w)); b2.Start(); However the portion is not locked. When I lock an arbitrary object (i.e. one created and not modified as object a=new object()) it locks well. Is boxing operation somehow "depromotes" my Object??
Tags (comma-separated)
Save Edits
Cancel