473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem logging from different threads.

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?

May 30 '06 #1
6 4187
The source code? Please provide a small example to reproduce the
problem.

Dave wrote:
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?


May 30 '06 #2
Dave wrote:
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
May 30 '06 #3

"Dave" <dc*****@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
|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.
May 31 '06 #4
Willy Denoyette [MVP] wrote:
"Dave" <dc*****@gmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
|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.
May 31 '06 #5
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.

Jun 1 '06 #6

"Dave" <dc*****@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
|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.
Jun 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Huzefa | last post by:
I am working on a amll project in Java that includes many classes. Each of the classes has a Logger object. I have associated a FileHandler with each of these Logger objects. The file is the same...
2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
0
by: dede | last post by:
Dear community, having written a working example for using Semphores on a windows client, I created a little "server-application" that does the following: A Server continously "listens/tails"...
4
by: Navin Mishra | last post by:
Hi, Does someone has an example of logging in a different thread ? Native Windows API had a mechanism to send thread message(which could be used to send a message with log data details). Does...
28
by: Martin | last post by:
hi i have written a logger with the following calling syntax: rlog << "bla" << var << .... i.e. just like cout. it is done by inheriting from std::streambuf and creating an ostream object...
6
by: pmatos | last post by:
Hi all, I am trying to create a simple but efficient C++ logging class. I know there are lots of them out there but I want something simple and efficient. The number one requirement is the...
16
by: Einar Høst | last post by:
Hi, I'm getting into the Trace-functionality in .NET, using it to provide some much-needed logging across dlls in the project we're working on. However, being a newbie, I'm wondering if some...
3
by: Ross Boylan | last post by:
I would like my different threads to log without stepping on each other. Past advice on this list (that I've found) mostly says to send the messages to a Queue. That would work, but bypasses...
2
by: ZHENG Zhong | last post by:
Hi, I implemented a small logging library with the API like this: logger& log = log_manager::instance().get_logger("my_logger"); log.stream(DEBUG) << "this is a debug message" << std::endl;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.