Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

console out in multi-threaded applications

Usually developing applications I am used to print to console in order to get useful debugging/tracing information. The application I am working now since it is multi-threaded sometimes I see my printf overlapping each other.

I tried to synchronize the screen using a mutex but I end up in slowing and blocking the app. How to solve this issue?

I am aware of MT logging libraries but in using them, since I log too much, I slow ( a bit ) my app.

I was thinking to the following idea..instead of logging within my applications why not log outside it? I would like to send logging information via socket to a second application process that actually print out on the screen.

Are you aware of any library already doing this? I use Linux/gcc.

thanks

afg

like image 930
Abruzzo Forte e Gentile Avatar asked Dec 13 '22 14:12

Abruzzo Forte e Gentile


1 Answers

You have 3 options. In increasing order of complexity:

  1. Just use a simple mutex within each thread. The mutex is shared by all threads.
  2. Send all the output to a single thread that does nothing but the logging.
  3. Send all the output to a separate logging application.

Under most circumstances, I would go with #2. #1 is fine as a starting point, but in all but the most trivial applications you can run in to problems serializing the application. #2 is still very simple, and simple is a good thing, but it is also quite scalable. You still end up doing the processing in the main application, but for the vast majority of applications you gain nothing by spinning this off to it's own, dedicated application.

Number 3 is what you're going to do in preformance-critical server type applications, but the minimal performance gain you get with this approach is 1: very difficult to achieve, 2: very easy to screw up, and 3: not the only or even most compelling reason people generally take this approach. Rather, people typically take this approach when they need the logging service to be seperated from the applications using it.

like image 197
John Dibling Avatar answered Dec 29 '22 13:12

John Dibling