Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #545

Re: Thread safe logging (or protecting global variables)

From Edek <edek.pienkowski@gmail.com>
Newsgroups comp.programming.threads
Subject Re: Thread safe logging (or protecting global variables)
Date 2011-09-07 10:34 +0200
Organization ATMAN - ATM S.A.
Message-ID <j47ah3$t3k$1@node2.news.atman.pl> (permalink)
References <Co89q.7290$cb.3785@newsfe14.ams2> <j434mo$6cj$1@node2.news.atman.pl> <49s9q.1564$8D1.58@newsfe19.ams2>

Show all headers | View raw


On 09/06/2011 06:48 PM, Jef Driesen wrote:
> On 09/05/2011 08:30 PM, Edek wrote:
>> On 09/05/2011 08:19 PM, Jef Driesen wrote:
[...]


>>
>> That will not cause any trouble, as long you don't have anything that
>> tries to do logging before that - think other initialisations, if there
>> is any C++. If this is pure C, things are easier, assuming everybody
>> reads the API spec saying "call the init() first". The last can be
>> achieved by another flag used only for the purpose of detecting any
>> use of logging mechanisms before init(). Naturally, the flag won't
>> really be thread safe, but after all it is not for being really used;
>> it depends if you want this mechanism or not, possibly #ifdef-ed.
>
[...]

> However my message_set_*() functions are entirely optional. By default,
> the logging should be disabled and you only need to call them if you
> want to enable the logging. So they are not true initialization
> functions for the library.

They are not: add an init().

What I meant to say in my original text is
> that *IF* you call these functions you have to call them before/after
> any threads are created/destroyed. I'm not sure you interpreted my post
> this way.

I proposed adding a new init() function.

>
> I think that makes a very important difference, because I believe I
> would still need some global flag to track whether the functions have
> been called or not (and thus initialized the mutex or not), which is
> again unsafe?

That's what I wrote too: but this flag only needs to be in debug
builds. It is meant to fire an assertion immediately.

>
>>> Can I even go one step futher and use simple a simple flag (like the
>>> g_loglevel variable above) that can only be modified from the
>>> message_set_*() functions, and which is only checked in the message()
>>> function but not modified.
>>
>> Will work on x86, but there is no guarantee it won't break on any other
>> CPU or on some new hardware a couple years later.
>>
>> An atomic would be enough - either the new standard, or find an
>> assembly that makes one for the target CPU, a couple of libraries have
>> been doing that for a long time. For example, logging libraries.
>
> I understand (in theory at least) how atomic operations can be used to
> protect a single variable like the g_loglevel flag in my example code.
> But how does this work (if that's even possible) if the logging state
> consists of multiple variables?

I do not see why a logging library would need to access atomically
a structure containing both log levels and the sink.

If you do not care about optimisations (for the 'logging off' case),
then you can create a larger structure and make it atomic.

Atomics are supported by CPUs up to some size; if larger they need to be
emulated, usually by locking a mutex (or a spinlock) and using
the data in the structure

Edek


Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-05 20:19 +0200
  Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-05 20:30 +0200
    Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-06 18:48 +0200
      Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-07 10:34 +0200
        Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-08 22:00 +0200
          Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-09 18:13 +0200
            Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-13 17:58 +0200
              Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-13 13:18 -0700
                Re: Thread safe logging (or protecting global variables) Jef Driesen <jefdriesen@hotmail.com.invalid> - 2011-09-14 07:16 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:18 -0700
                Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-14 09:33 +0200
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-14 13:17 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:03 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 13:19 -0700
                Re: Thread safe logging (or protecting global variables) "James" <no@spam.invalid> - 2011-09-14 19:19 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-14 21:03 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 13:12 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 12:24 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 22:22 +0200
                Re: Thread safe logging (or protecting global variables) "James" <no@spam.invalid> - 2011-09-15 13:33 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 15:36 -0700
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-15 15:36 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-16 16:28 +0200
                Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-16 15:06 -0700
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-17 12:50 +0200
                Re: Thread safe logging (or protecting global variables) Alexander Terekhov <terekhov@web.de> - 2011-09-15 12:50 +0200
              Re: Thread safe logging (or protecting global variables) Edek <edek.pienkowski@gmail.com> - 2011-09-14 09:17 +0200
  Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 16:28 -0700
    Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 16:33 -0700
      Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-08 20:55 -0700
    Re: Thread safe logging (or protecting global variables) Joshua Maurice <joshuamaurice@gmail.com> - 2011-09-12 00:45 -0700

csiph-web