Welcome to Web-News
A Web-based News Reader
Subject Re: D Mutex reference.
From Alexander Panek <a.panek@brainsware.org>
Date Fri, 19 Jan 2007 03:29:37 +0100
Newsgroups digitalmars.D.learn

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


Recent messages in this thread
 
-# D Mutex reference. Saaa 18-Jan-2007 08:23 pm
.-# Re: D Mutex reference. Alexander Panek 18-Jan-2007 08:31 pm
..-# Re: D Mutex reference. Saaa 18-Jan-2007 08:47 pm
...-# Re: D Mutex reference. (Current message) Alexander Panek 18-Jan-2007 09:29 pm
....-# Re: D Mutex reference. Saaa 19-Jan-2007 07:03 am
.....-# Re: D Mutex reference. Frits van Bommel 19-Jan-2007 07:16 am
......-# Re: D Mutex reference. Saaa 19-Jan-2007 08:44 am
.......|# Re: D Mutex reference. Alexander Panek 19-Jan-2007 08:54 am
.......\# Re: D Mutex reference. Frits van Bommel 19-Jan-2007 11:06 am