Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #535
| From | Edek <edek.pienkowski@gmail.com> |
|---|---|
| Newsgroups | comp.programming.threads |
| Subject | Re: Thread safe logging (or protecting global variables) |
| Date | 2011-09-05 20:30 +0200 |
| Organization | ATMAN - ATM S.A. |
| Message-ID | <j434mo$6cj$1@node2.news.atman.pl> (permalink) |
| References | <Co89q.7290$cb.3785@newsfe14.ams2> |
On 09/05/2011 08:19 PM, Jef Driesen wrote: > Hi, > > I have a C library that is supposed to be thread-safe under the > condition that you access each library object only from a single thread. > This is done by ensuring that objects do not share any state (e.g. no > global variables) and where necessary reentrant variants of certain C > library functions are used (e.g. localtime_r). > > Now, I want to add a logging feature for debugging purposes. The idea > would be to implement a logging infrastructure where the amount of > output can be controlled with a loglevel parameter (defaulting to no > output) and redirected to a file (defaulting to stderr), like in the > pseudo code below: > [...] > > Obviously, this is not thread-safe due to the use of global variables. > Thus my next idea was to add a global mutex (e.g. pthread_mutex_t on > unix or CRITICAL_SECTION on windows) protecting these global variables. > But how do I initialize the mutex, considering that my library doesn't > have (or need) any initialization functions? For the unix pthread there > is PTHREAD_MUTEX_INITIALIZER, but as far as I know there is no such > thing for a windows critical section? How can this problem be solved? By definition ;): if you're trying to use threads on win32, you are screwed. There are too many wise people there. > > Would it be an acceptable solution to mark the message_set_*() > functions as not thread-safe and require applications to call them > before/after any threads are created/destroyed? (For my use case such a > restriction would be acceptable, although I'm not sure it would be > fully thread-safe.) Then those functions could initialize the mutex. Or > will that cause any trouble? 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. > > 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. Edek
Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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