Difference between revisions of "CSharp Locking and Inter Thread Communication"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with "=Links= * http://www.albahari.com/threading/part4.aspx * http://stackoverflow.com/questions/15164408/lock-free-concurrent-queue * http://stackoverflow.com/questions/505515/c...") |
PeterHarding (talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 25: | Line 25: | ||
} | } | ||
} | } | ||
</pre> | |||
=Another Example= | |||
Found this one here - http://stackoverflow.com/questions/505515/c-sharp-thread-safety-with-get-set | |||
The nitty gritty: | |||
<pre> | |||
public class Status | |||
{ | |||
private int _code; | |||
private DateTime _lastUpdate; | |||
private object _sync = new object(); | |||
public int Code | |||
{ | |||
get { lock (_sync) { return _code; } } | |||
set | |||
{ | |||
lock (_sync) { | |||
_code = value; | |||
} | |||
// Notify listeners | |||
EventHandler handler = Changed; | |||
if (handler != null) { | |||
handler(this, null); | |||
} | |||
} | |||
} | |||
public DateTime LastUpdate | |||
{ | |||
get { lock (_sync) { return _lastUpdate; } } | |||
set { lock (_sync) { _lastUpdate = value; } } | |||
} | |||
public event EventHandler Changed; | |||
} | |||
</pre> | |||
A 'Polling' thread would us this as follows: | |||
<pre> | |||
Status status = new Status(); | |||
ManualResetEvent changedEvent = new ManualResetEvent(false); | |||
Thread worker = new Thread( | |||
delegate() { | |||
status.Changed += delegate { changedEvent.Set(); }; | |||
while (true) { | |||
changedEvent.WaitOne(Timeout.Infinite); | |||
int code = status.Code; | |||
DateTime lastUpdate = status.LastUpdate; | |||
changedEvent.Reset(); | |||
} | |||
} | |||
); | |||
worker.Start(); | |||
</pre> | </pre> | ||
[[Category:CSharp]] | [[Category:CSharp]] |
Latest revision as of 11:34, 16 March 2014
Links
- http://stackoverflow.com/questions/15164408/lock-free-concurrent-queue
- http://stackoverflow.com/questions/505515/c-sharp-thread-safety-with-get-set
- http://stackoverflow.com/questions/251391/why-is-lockthis-bad
- http://www.codeproject.com/Articles/8018/Bounded-Blocking-Queue-One-Lock
- http://stackoverflow.com/questions/6029804/how-does-lock-work-exactly
bool lockWasTaken = false; var temp = obj; try { Monitor.Enter(temp, ref lockWasTaken); // body } finally { if (lockWasTaken) { Monitor.Exit(temp); } }
Another Example
Found this one here - http://stackoverflow.com/questions/505515/c-sharp-thread-safety-with-get-set
The nitty gritty:
public class Status { private int _code; private DateTime _lastUpdate; private object _sync = new object(); public int Code { get { lock (_sync) { return _code; } } set { lock (_sync) { _code = value; } // Notify listeners EventHandler handler = Changed; if (handler != null) { handler(this, null); } } } public DateTime LastUpdate { get { lock (_sync) { return _lastUpdate; } } set { lock (_sync) { _lastUpdate = value; } } } public event EventHandler Changed; }
A 'Polling' thread would us this as follows:
Status status = new Status(); ManualResetEvent changedEvent = new ManualResetEvent(false); Thread worker = new Thread( delegate() { status.Changed += delegate { changedEvent.Set(); }; while (true) { changedEvent.WaitOne(Timeout.Infinite); int code = status.Code; DateTime lastUpdate = status.LastUpdate; changedEvent.Reset(); } } ); worker.Start();