While I was reading about `ReaderWriterLockSlim` lock mechanism , [There was this guy](https://stackoverflow.com/a/5188627/859154) who suggested that `Interlock` Functions can be used for a finer locking

Also, I found [here](https://stackoverflow.com/a/4217515/859154) another answer from Marc : 

> 
> ...Writes make their change to a cloned copy, then use
>  Interlocked.CompareExchange to swap the reference (re-applying their
>  change if another thread mutated the reference in the interim).
> 
> 
> 

Well , Currently all I know about `Interlocked` object , is that it is used (in a multithreaded environment) to do atomic `additions`, `compare` , `compareExchange` operations. (and I know how to use it)

But (*and here is my question*) —

**Question**

How can I use it as a *lock* ? ( sample code will be much appreciated)

edit
====

For simplicity , I'm pasting this code ( *which is not thread safe - if `Go` was called by two threads simultaneously, it would be possible to get a division-by-zero error*) : 

```
class ThreadUnsafe
 {
   static int _val1 = 1, _val2 = 1;

   static void Go()
       {
        if (_val2 != 0) Console.WriteLine (_val1 / _val2);
        _val2 = 0;
       }
}

```

How can I use Interlock to replace the lock (which would have solved the problem) ?