Problem logging from different threads.

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I have a service that has 6 different threads. Each thread has a timer
on it that elapses at about the same time (if not the same time). When
the timer elapses I am trying to log a message by writing to a text
file. The problem is that not every thread is able to log its message.
I thought it was because each thread is trying to use the same
resource. So i put in mutex.waitone() and released the mutex at the
end of the method. It still doesn't log all the messages. Is there
something that I am missing?
 
Dave said:
I have a service that has 6 different threads. Each thread has a timer
on it that elapses at about the same time (if not the same time). When
the timer elapses I am trying to log a message by writing to a text
file. The problem is that not every thread is able to log its message.
I thought it was because each thread is trying to use the same
resource. So i put in mutex.waitone() and released the mutex at the
end of the method. It still doesn't log all the messages. Is there
something that I am missing?
I have a similar situation, except I may potentially have more threads
(depending on load). In my situation it logs everything perfectly fine.

I simply put a lock {...} around the code and seems to do the trick.

Regards
 
|I have a service that has 6 different threads. Each thread has a timer
| on it that elapses at about the same time (if not the same time). When
| the timer elapses I am trying to log a message by writing to a text
| file. The problem is that not every thread is able to log its message.
| I thought it was because each thread is trying to use the same
| resource. So i put in mutex.waitone() and released the mutex at the
| end of the method. It still doesn't log all the messages. Is there
| something that I am missing?
|

Hard to tell without seeing some code, anyway I would suggest you have a
single thread that manages the logging activity, and have the "worker"
threads transfer the log messages to the "Log writer" thread using a simple
producer-consumer queue.

Willy.
 
Willy said:
|I have a service that has 6 different threads. Each thread has a timer
| on it that elapses at about the same time (if not the same time). When
| the timer elapses I am trying to log a message by writing to a text
| file. The problem is that not every thread is able to log its message.
| I thought it was because each thread is trying to use the same
| resource. So i put in mutex.waitone() and released the mutex at the
| end of the method. It still doesn't log all the messages. Is there
| something that I am missing?
|

Hard to tell without seeing some code, anyway I would suggest you have a
single thread that manages the logging activity, and have the "worker"
threads transfer the log messages to the "Log writer" thread using a simple
producer-consumer queue.

Willy,

I've heard you mention the producer-consumer queue (and post code)
several times now. Do you have a specific real-life example of its
usage that you can post? For instance, to resolve the problem that this
gentleman is talking about. Or the problem of getting IP messages on
different threads and serializing them to a single thread.

Thanks.
 
I was able to create a work around for my problem by starting each
timer 1 second apart from each other. But I would still like to know
how to do this without the workaround. Here is a code example:

Thread thread = new Thread( new ThreadStart(MonitorThread) )
thread.Start();

private void MonitorThread()
{
MyObject obj1 = new MyObject();
MyObject obj2 = new MyObject();
MyObject obj3 = new MyObject();
MyObject obj4 = new MyObject();
MyObject obj5 = new MyObject();
MyObject obj6 = new MyObject();
}

public class MyObject
{
public MyObject()
{
Timer timer = new Timer(x);
timer.Elapsed += new ElapsedEventHandler(timerElapsed);
timer.Start();
}
private void timerElapsed(object sender, ElapsedEventArgs e)
{
mutex.waitone();
StreamWriter swLog = new StreamWriter(path, true);
swLog.AutoFlush = true;
swLog.WriteLine(DateTime.Now.ToString() + ": " + message);
swLog.Close();
mutex.ReleaseMutex();
}
}

So basically each timer is a seperate thread. Those are the threads
that im talking about. But each timer is started from the same thread.
Help is appreciated.
 
|I was able to create a work around for my problem by starting each
| timer 1 second apart from each other. But I would still like to know
| how to do this without the workaround. Here is a code example:
|
| Thread thread = new Thread( new ThreadStart(MonitorThread) )
| thread.Start();
|
| private void MonitorThread()
| {
| MyObject obj1 = new MyObject();
| MyObject obj2 = new MyObject();
| MyObject obj3 = new MyObject();
| MyObject obj4 = new MyObject();
| MyObject obj5 = new MyObject();
| MyObject obj6 = new MyObject();
| }
|
| public class MyObject
| {
| public MyObject()
| {
| Timer timer = new Timer(x);
| timer.Elapsed += new ElapsedEventHandler(timerElapsed);
| timer.Start();
| }
| private void timerElapsed(object sender, ElapsedEventArgs e)
| {
| mutex.waitone();
| StreamWriter swLog = new StreamWriter(path, true);
| swLog.AutoFlush = true;
| swLog.WriteLine(DateTime.Now.ToString() + ": " + message);
| swLog.Close();
| mutex.ReleaseMutex();
| }
| }
|
| So basically each timer is a seperate thread. Those are the threads
| that im talking about. But each timer is started from the same thread.
| Help is appreciated.
|

Though I'm not clear on what you are trying to achieve in your design,
following is something I hacked together just to give you an idea of what
you can achieve using a simple Producer/Consumer queue.
Note that while the timers happen to run on a thread from the pool, they
don't necessarely run on different threads, all depends on the number of
CPU's in the box, the frequency of generated timer events and the service
time of the timer delegate, most of the time they are serviced by the same
couple of threads.

using System;
using System.IO;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
class Program
{
private static EventWaitHandle evntWh;
static void Main()
{
Program prog = new Program();
LoggerQueue<string> logMessageQueue = new LoggerQueue<string>();
evntWh = new EventWaitHandle(true, EventResetMode.ManualReset);
// start a consumer
Thread loggerThread = new Thread((ThreadStart) delegate {
prog.Consume(logMessageQueue); });
loggerThread.Start();
MyObject obj1 = new MyObject(logMessageQueue);
MyObject obj2 = new MyObject(logMessageQueue);
MyObject obj3 = new MyObject(logMessageQueue);
MyObject obj4 = new MyObject(logMessageQueue);
MyObject obj5 = new MyObject(logMessageQueue);
MyObject obj6 = new MyObject(logMessageQueue);
Console.ReadLine();
// Request Consumer thread to terminate
evntWh.Reset();
loggerThread.Join();
}

private void Consume(LoggerQueue<string> q) {
using (StreamWriter swLog = new StreamWriter("LogFile.txt")) {
swLog.AutoFlush = true;
string message;
// dequeu messages until requested to return
while (evntWh.WaitOne(100, false)) {
if((message = q.Dequeue()) != null)
swLog.WriteLine(message);
}
}
}
}

class MyObject
{
static int m_numMessagesPosted;
LoggerQueue<string> q;
System.Timers.Timer timer;
public MyObject(LoggerQueue<string> logMessageQueue) {
q = logMessageQueue;
Random r = new Random((int) DateTime.Now.Ticks);
timer = new System.Timers.Timer((double)r.Next(100, 2000));
timer.Elapsed += new System.Timers.ElapsedEventHandler(Produce);
timer.Start();
}
private void Produce(object sender, System.Timers.ElapsedEventArgs e) {
// enqueue a simple string message
string s = String.Format("Logmessage {0} threadId {1} - {2}",
Interlocked.Increment(ref m_numMessagesPosted),
Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
q.Enqueue(s);
}
}

class LoggerQueue<T>
{
private int _count = 0;
private Queue<T> _queue = new Queue<T>();

public T Dequeue()
{
lock (_queue)
{
while (_count <= 0)
{
// wait a maximum of x seconds, and return when no message queued so
far
if(!Monitor.Wait(_queue, 1000))
return default(T);
}
_count--;
return _queue.Dequeue();
}
}
public void Enqueue(T data)
{
if (data == null) throw new ArgumentNullException("data");
lock (_queue)
{
_queue.Enqueue(data);
_count++;
Monitor.PulseAll(_queue);
}
}
}


Willy.
 

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

Back
Top