View mode: basic / threaded / horizontal-split · Log in · Help
August 14, 2005
synchronized keyword
I'm relatively new to D, but I’d just like to say kudos to Mr. Bright for
creating such a wonderful language!

I just ran across an interesting issue.  I may have missed something somewhere,
but, I had a function inside of a class declared:

public class Logger {
..
public synchronized void log(char s[]) {
printf("The log entry is %.*s\n", s);
}
..
}

I was under the impression that this would safely lock a mutex so that it would
be thread safe (as it would in Java).  However, I found instances where the
output of two threads were happening at the same time, producing garbled output.

I changed the code to:
public void log(char s[]) {
synchronized {
printf("The log entry is %.*s\n", s);
}
}

Which seemed to work as expected.

So my question is, what does putting synchronized in the declaration do, and why
doesn't it give a warning or error if it is not supposed to be there?

Also, has there been any thought given to allowing an exception to produce a
stack trace, if compiled in debug mode?  This could help tremendously in
tracking down where certain exceptions are occurring (especially access
violation!).  If there is a specific reason for not doing it, how about allowing
for a __FILE__/__LINE__ type of directive, so that when throwing an exception,
one can include the line number/source file in the message?





-RAL
August 14, 2005
Re: synchronized keyword
On Sun, 14 Aug 2005 04:40:56 +0000 (UTC), Robert Libby wrote:

> I'm relatively new to D, but I’d just like to say kudos to Mr. Bright for
> creating such a wonderful language!
> 
> I just ran across an interesting issue.  I may have missed something somewhere,
> but, I had a function inside of a class declared:
> 
> public class Logger {
> ..
> public synchronized void log(char s[]) {
> printf("The log entry is %.*s\n", s);
> }
> ..
> }
> 
> I was under the impression that this would safely lock a mutex so that it would
> be thread safe (as it would in Java).  However, I found instances where the
> output of two threads were happening at the same time, producing garbled output.
> 
> I changed the code to:
> public void log(char s[]) {
> synchronized {
> printf("The log entry is %.*s\n", s);
> }
> }
> 
> Which seemed to work as expected.
> 
> So my question is, what does putting synchronized in the declaration do,

Nothing.

> and why doesn't it give a warning or error if it is not supposed to be there?

This is Walter's style. Many (but not all) attribute and statement
qualifications can appear in places that they do not apply to. This is to
make coding a bit easier by just ignoring inappropriate placements. 

For example:

public void log(char s[]) 
{
  synchronized 
  {
     printf("The log entry is %.*s\n", s);
     void InnerFunc(int abc) { . . .  }
  }
}

In this case, synchronized doesn't apply to the inner function, but it
doesn't issue an error message either.


> Also, has there been any thought given to allowing an exception to produce a
> stack trace, if compiled in debug mode?  This could help tremendously in
> tracking down where certain exceptions are occurring (especially access
> violation!).  

Just hasn't hit the top of the TODO list yet.

>If there is a specific reason for not doing it, how about allowing
> for a __FILE__/__LINE__ type of directive, so that when throwing an exception,
> one can include the line number/source file in the message?

These special token have been defined for us ...

__FILE__ 	string literal containing source file name 	
__LINE__ 	integer literal of the current source line number 	
__DATE__ 	string literal of the date of compilation "mmm dd yyyy" 	
__TIME__ 	string literal of the time of compilation "hh:mm:ss" 	
__TIMESTAMP__ 	string literal of the date and time of compilation 
                  "www mmm dd hh:mm:ss yyyy"

Look in the 'Lexical' section of the documentation.

-- 
Derek Parnell
Melbourne, Australia
14/08/2005 7:43:29 PM
August 14, 2005
Re: synchronized keyword
In article <1hfji2l2oyhja.dthmp6jgk8yx.dlg@40tude.net>, Derek Parnell says...
>
>On Sun, 14 Aug 2005 04:40:56 +0000 (UTC), Robert Libby wrote:
>
>> I'm relatively new to D, but I’d just like to say kudos to Mr. Bright for
>> creating such a wonderful language!
>> 
>> I just ran across an interesting issue.  I may have missed something somewhere,
>> but, I had a function inside of a class declared:
>> 
>> public class Logger {
>> ..
>> public synchronized void log(char s[]) {
>> printf("The log entry is %.*s\n", s);
>> }
>> ..
>> }
>> 
>> I was under the impression that this would safely lock a mutex so that it would
>> be thread safe (as it would in Java).  However, I found instances where the
>> output of two threads were happening at the same time, producing garbled output.
>> 
>> I changed the code to:
>> public void log(char s[]) {
>> synchronized {
>> printf("The log entry is %.*s\n", s);
>> }
>> }
>> 
>> Which seemed to work as expected.
>> 
>> So my question is, what does putting synchronized in the declaration do,
>
>Nothing.
>
>> and why doesn't it give a warning or error if it is not supposed to be there?
>
>This is Walter's style. Many (but not all) attribute and statement
>qualifications can appear in places that they do not apply to. This is to
>make coding a bit easier by just ignoring inappropriate placements. 
>
>For example:
>
> public void log(char s[]) 
>{
>   synchronized 
>   {
>      printf("The log entry is %.*s\n", s);
>      void InnerFunc(int abc) { . . .  }
>   }
> }
>
>In this case, synchronized doesn't apply to the inner function, but it
>doesn't issue an error message either.
>
>
>> Also, has there been any thought given to allowing an exception to produce a
>> stack trace, if compiled in debug mode?  This could help tremendously in
>> tracking down where certain exceptions are occurring (especially access
>> violation!).  
>
>Just hasn't hit the top of the TODO list yet.
>
>>If there is a specific reason for not doing it, how about allowing
>> for a __FILE__/__LINE__ type of directive, so that when throwing an exception,
>> one can include the line number/source file in the message?
>
>These special token have been defined for us ...
>
>__FILE__ 	string literal containing source file name 	
>__LINE__ 	integer literal of the current source line number 	
>__DATE__ 	string literal of the date of compilation "mmm dd yyyy" 	
>__TIME__ 	string literal of the time of compilation "hh:mm:ss" 	
>__TIMESTAMP__ 	string literal of the date and time of compilation 
>                   "www mmm dd hh:mm:ss yyyy"
>
>Look in the 'Lexical' section of the documentation.
>
>-- 
>Derek Parnell
>Melbourne, Australia
>14/08/2005 7:43:29 PM

Thanks for the information, Derek. I guess I never got to the bottom of the
Lexical section of the documentation!


-Robert
August 14, 2005
Re: synchronized keyword
Derek Parnell wrote:
> On Sun, 14 Aug 2005 04:40:56 +0000 (UTC), Robert Libby wrote:
> 
>>I just ran across an interesting issue.  I may have missed something somewhere,
>>but, I had a function inside of a class declared:
>>
>>public class Logger {
>>..
>>public synchronized void log(char s[]) {
>>printf("The log entry is %.*s\n", s);
>>}
>>..
>>}
>>
>>I was under the impression that this would safely lock a mutex so that it would
>>be thread safe (as it would in Java).  However, I found instances where the
>>output of two threads were happening at the same time, producing garbled output.
>>
>>I changed the code to:
>>public void log(char s[]) {
>>synchronized {
>>printf("The log entry is %.*s\n", s);
>>}
>>}
>>
>>Which seemed to work as expected.
>>
>>So my question is, what does putting synchronized in the declaration do,
> 
> 
> Nothing.
> 
Does nothing? But is it a bug or a TODO feature? Because in the doc 
Overview (and also on the SDWest 2004 Paper) the following is stated: 
"Synchronization can be done at either the method or the object level.
	synchronized int func() { . }"


-- 
Bruno Medeiros
Computer Science/Engineering student
March 22, 2007
Re: synchronized keyword
Robert Libby wrote:
> I'm relatively new to D, but I’d just like to say kudos to Mr. Bright for
> creating such a wonderful language!
> 
> I just ran across an interesting issue.  I may have missed something somewhere,
> but, I had a function inside of a class declared:
> 
> public class Logger {
> ..
> public synchronized void log(char s[]) {
> printf("The log entry is %.*s\n", s);
> }
> ..
> }
> 
> I was under the impression that this would safely lock a mutex so that it would
> be thread safe (as it would in Java).  However, I found instances where the
> output of two threads were happening at the same time, producing garbled output.
> 
> I changed the code to:
> public void log(char s[]) {
> synchronized {
> printf("The log entry is %.*s\n", s);
> }
> }
> 
> Which seemed to work as expected.

synchronized as a statement with no object in () means it syncs on a 
global mutex.

> So my question is, what does putting synchronized in the declaration do,

It synchronizes on the instance of Logger. You can see it in the 
generated code (try running obj2asm on it).

> and why
> doesn't it give a warning or error if it is not supposed to be there?

It not working on your code suggests that two different instances of 
Logger were calling log?
Top | Discussion index | About this forum | D home