Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 538189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:00:19+00:00 2026-05-13T10:00:19+00:00

I need to design a thread-safe logger. My logger must have a Log() method

  • 0

I need to design a thread-safe logger. My logger must have a Log() method that simply queues a text to be logged. Also a logger must be lock-free – so that other thread can log messages without locking the logger. I need to design a worker thread that must wait
for some synchronization event and then log all messages from the queue using standard .NET logging (that is not thread-safe). So what i am interested in is synchronization of worker thread – and Log function. Below is a sketch of the class that i designed. I think I must use Monitor.Wait/Pulse here or any other means to suspend and resume worker thread. I don;t want to spend CPU cycles when there is no job for logger.

Let me put it another way – I want to design a logger that will not block a caller threads that use it. I have a high performance system – and that is a requirement.

class MyLogger
{
  // This is a lockfree queue - threads can directly enqueue and dequeue
  private LockFreeQueue<String> _logQueue;
  // worker thread
  Thread _workerThread;
  bool _IsRunning = true;

 // this function is used by other threads to queue log messages
  public void Log(String text)
{
  _logQueue.Enqueue(text);
}

// this is worker thread function
private void ThreadRoutine()
{
 while(IsRunning)
 {
   // do something here
 }
}    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:00:19+00:00Added an answer on May 13, 2026 at 10:00 am

    “lock-free”does not mean that threads won’t block each other. It means that they block each other through very efficient but also very tricky mechanisms. Only needed for very high performance scenarios and even the experts get it wrong (a lot).

    Best advice: forget “lock-free”and just use a “thread-safe” queue.

    I would recommend the “Blocking Queue” from this page.

    And it’s a matter of choice to include the ThreadRoutine (the Consumer) in the class itself.

    To the second part of your question, it depends on what “some synchronization event” exactly is. If you are going to use a Method call, then let that start a one-shot thread. If you want to wait on a Semaphore than don’t use Monitor and Pulse. They are not reliable here. Use an AutoResetEvent/ManualResetEvent.
    How to surface that depends on how you want to use it.

    Your basic ingredients should look like this:

    class Logger
    {
        private AutoResetEvent _waitEvent = new AutoResetEvent(false);
        private object _locker = new object();
        private bool _isRunning = true;    
    
        public void Log(string msg)
        {
           lock(_locker) { _queue.Enqueue(msg); }
        }
    
        public void FlushQueue()
        {
            _waitEvent.Set();
        }
    
        private void WorkerProc(object state)
        {
            while (_isRunning)
            {
                _waitEvent.WaitOne();
                // process queue, 
                // ***
                while(true)
                {
                    string s = null;
                    lock(_locker)
                    {
                       if (_queue.IsEmpty) 
                          break;
                       s = _queue.Dequeu();
                    }
                    if (s != null)
                      // process s
                }
            } 
        }
    }
    

    Part of the discussion seems to be what to do when processing the Queue (marked ***). You can lock the Queue and process all items, during which adding of new entries will be blocked (longer), or lock and retrieve entries one by one and only lock (very) shortly each time. I’ve adde that last scenario.

    A summary: You don’t want a Lock-Free solution but a Block-Free one. Block-Free doesn’t exist, you will have to settle for something that blocks as little as possible. The last iteration of mys sample (incomplete) show how to only lock around the Enqueue and Dequeue calls. I think that will be fast enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.