D Mutex reference.

Alexander Panek a.panek at brainsware.org
Thu Jan 18 18:29:37 PST 2007


Saaa wrote:
>> http://digitalmars.com/d/statement.html#SynchronizedStatement
>>
>> Does this one help?
> 
> Thanks for the reply.
> I have one thread generating alot of data and main reading a few of them 
> every 10ms or so.
> This can be a problem, right? (when main tries to read some data which 
> hasn't been writting completely?)

Actually, yes.

> Reading about multi threading pointed me to mutexes, but I can't find any 
> reference.
> Synchronized only locks a part of the code, not the actual data? 

The point of synchronized blocks is, that you can finish what ever you 
do with your data, without getting interrupted by another thread.

There are two ways to use synchronized:

a) standalone, so it's like Windows' critical sections:

synchronized {
     /* ... code ... */
}

b) synchronized with an object

"synchronized (Expression), where Expression evaluates to an Object 
reference, allows only one thread at a time to use that Object to 
execute the ScopeStatement. If Expression is an instance of an 
Interface, it is cast to an Object."

synchronized ( /* expression or object */ ) {
     /* ... code ... */
}

Apparently, b) should be the way to go for you.

Best regards,
Alex



More information about the Digitalmars-d-learn mailing list