I need to Monitor.Lock() a native type, not an object. How can I? 
Author Message
 I need to Monitor.Lock() a native type, not an object. How can I?

I have a server component that will be handing out unique IDs.  The ID is
implemented as a simple counter, but I need to lock it down while I'm
handing out a new ID.  I originally tried to do a Monitor.Lock() on it, but
when I Monitor.Exit() I get a
"System.Threading.SynchronizationLockException: Object synchronization
method was called from an unsynchronized block of code" exception.

Can I lock variable of native type?



Mon, 01 Nov 2004 03:40:48 GMT  
 I need to Monitor.Lock() a native type, not an object. How can I?
You can create a dummy object to serve as the locker for your native type.

int i=0;
object dummy = new object();

lock(dummy){
    i++;
    i++;
    i++;

Quote:
}

lock(dummy){
    Console.WriteLine(i);

Quote:
}



Quote:
> I have a server component that will be handing out unique IDs.  The ID is
> implemented as a simple counter, but I need to lock it down while I'm
> handing out a new ID.  I originally tried to do a Monitor.Lock() on it,
but
> when I Monitor.Exit() I get a
> "System.Threading.SynchronizationLockException: Object synchronization
> method was called from an unsynchronized block of code" exception.

> Can I lock variable of native type?



Mon, 01 Nov 2004 04:15:27 GMT  
 I need to Monitor.Lock() a native type, not an object. How can I?
0to60,
The easiest way is to use the System.Threading.Interlocked class.

- CompareExchange
- Decrement
- Exchange
- Increment

I suspect you will want Increment.

Hope this helps
Jay


Quote:
> I have a server component that will be handing out unique IDs.  The ID is
> implemented as a simple counter, but I need to lock it down while I'm
> handing out a new ID.  I originally tried to do a Monitor.Lock() on it,
but
> when I Monitor.Exit() I get a
> "System.Threading.SynchronizationLockException: Object synchronization
> method was called from an unsynchronized block of code" exception.

> Can I lock variable of native type?



Mon, 01 Nov 2004 10:20:22 GMT  
 I need to Monitor.Lock() a native type, not an object. How can I?

Quote:
> You can create a dummy object to serve as the locker for your native type.

> int i=0;
> object dummy = new object();

> lock(dummy){
>     i++;
>     i++;
>     i++;
> }
> lock(dummy){
>     Console.WriteLine(i);
> }

How does that lock down i?


Mon, 01 Nov 2004 22:12:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. size_t - surely not a native data type ?

2. Lock Statement behavior with other objects within the locked object, and enumerators

3. Type Casting between Base and Derived Type (Serialization of objects with Type members)

4. Multimonitoring Win98, but locking second monitor

5. Does Monitor.Wait() release the lock()?

6. Type Info, Parameter Info Not working while mouse is over an object or method

7. FOREACH does not determine Object type?

8. ADO and native C++ data types?

9. Why use BOOL versus native data type bool?

10. Size of native types

11. Linker error "relocations remain", shared object, Solaris, gcc, ld, java, java native interface, cplex

12. event_source(native) not injecting event initialization code into constructor always

 

 
Powered by phpBB® Forum Software