Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Sugguest: turn Stdout to thread-safe

Moderators: kris

Posted: 09/13/08 14:22:00

As a common used object, I think turn Stdout to thread-safe can give us many help, do you think so ?

Author Message

Posted: 09/13/08 14:32:08

As a commonly used object, it would be way too slow to have Stdout thread-safe :) If you need that, use tango.util.log.Trace instead.

Posted: 09/14/08 02:17:32

In my group, some programmer have c/c++ experiense, they use printf, cout in multi thread program when they use c/c++.

When they turn to d, they read the document and find Stdout, they try to use it in little test program, it is ok. And they remember it.

Next time, they need to do output in a real project(multi thread): "Oh, printf --- Stdout", ok, it is.

So, problem occured, and, cause wired things to debug :(

If Stdout keep to be thread-unsafe, I think " Stdout/Stderr, the standard way for formatted console output. This is Tango's printf. "

should metion this, like that, "This is Tangos printf --- BUT THREAD-UNSAFE ! "

My opinion is: provide a safe default Stdout( D's printf), and a fast thread-unsafe version as backup.

Posted: 09/14/08 05:32:25 -- Modified: 09/14/08 05:35:01 by
DiscipleRayne

Why not just use tango.util.log.Trace instead?

Posted: 09/14/08 06:45:27 -- Modified: 09/14/08 06:47:09 by
kris

yes, or better yet, use tango.util.log instead. Takes a little more to setup a configurable logging system (two imports) but it is generally much more flexible than using something like Stdout (and is thread-safe too)

Although it is just slightly out of date, see the chapter on logging

Posted: 09/14/08 07:34:22 -- Modified: 09/14/08 07:35:37 by
redsea

I know logging library. Even before the logging library occur (when I use 0.96), I create a sync version of Stdout, and I can use it correctly.

Because c/c++ has no standard log libary, they use printf to output debug and log information. When they turn to D, they read Stdout is printf, and use it to do such thing many times.

I grep Stdout in serval project, found many misuse, it is dangerous and cause bugs hard to find (I doubt gc code last time.)

If the default Stdout keep cause issues, I would like to delete Stdout.d in our standard d environment.

Posted: 09/14/08 07:37:26

Our workmate use c/python, and now d. They use c to develop linux kernel mode (printk), nginx module (printf), and such on.

Posted: 09/14/08 19:26:12

I believe that whether printf is thread safe is platform dependent. Or even dependent on which version of the C stdlib you link against, like libc.lib or libcmt.lib on Windows.

Posted: 09/14/08 20:14:30

Tests have shown that making Stdout threadsafe triples its execution time. For almost all cases, that simply isn't worth it. But since that is needed from time to time, there is tango.util.log.Trace for you. And as mentioned, there are other alternatives too.

Posted: 09/15/08 03:04:18

torhu wrote:

I believe that whether printf is thread safe is platform dependent. Or even dependent on which version of the C stdlib you link against, like libc.lib or libcmt.lib on Windows.

libc itself, is not for develop multi thread programm. libcmt's printf is thread safe. gcc's printf is thread self.

Indeed, the function in <stdio.h> is all thread safe.

Posted: 09/15/08 03:12:18

larsivi wrote:

Tests have shown that making Stdout threadsafe triples its execution time. For almost all cases, that simply isn't worth it. But since that is needed from time to time, there is tango.util.log.Trace for you. And as mentioned, there are other alternatives too.

Triple ? That is much larger than I expected :(

But if only effect the filter program much, like grep, awk, uniq; other programss would not spend much time on stdio.