Global variable but no singleton

C

cppquester

I have a set of classes.
All should report (send messages) to the same logger object.

But for several instantiations of the set of classes each
instantiation should use a different logger.

I could put the logger into one of the classes and then set all other
classes belonging to the same instantiation to this logger. But it is
tidious and error prone.

Any suggestions?

Background is, that I use c++ and c# together and instantiate each set
of c++ classes within its own (c#) thread.
Using a singleton for the logger causes crashes due to accessing
confilcts.
I do not see a way to synchronize the c++ threads with each other if
they are called from (multi threaded) c#.

Thanks,
Marc
 
M

Michael Doubez

I have a set of classes.
All should report (send messages) to the same logger object.

But for several instantiations of the set of classes each
instantiation should use a different logger.

I could put the logger into one of the classes and then set all other
classes belonging to the same instantiation to this logger. But it is
tidious and error prone.

Any suggestions?

Background is, that I use c++ and c# together and instantiate each set
of c++ classes within its own (c#) thread.
Using a singleton for the logger causes crashes due to accessing
confilcts.
I do not see a way to synchronize the c++ threads with each other if
they are called from (multi threaded) c#.

AFAIS you have two solutions:
- make the logger thread-safe
- use thread local storage instance: Boost.Thread have support for
it with thread_specific_ptr but it is quite easy to roll your own.
Each thread will get its own global, which seems to be what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top