473,809 Members | 2,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4224
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.goo glegroups.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.goo glegroups.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(Mon itorThread) )
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 ElapsedEventHan dler(timerElaps ed);
timer.Start();
}
private void timerElapsed(ob ject sender, ElapsedEventArg s e)
{
mutex.waitone() ;
StreamWriter swLog = new StreamWriter(pa th, true);
swLog.AutoFlush = true;
swLog.WriteLine (DateTime.Now.T oString() + ": " + message);
swLog.Close();
mutex.ReleaseMu tex();
}
}

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.goo glegroups.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(Mon itorThread) )
| 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 ElapsedEventHan dler(timerElaps ed);
| timer.Start();
| }
| private void timerElapsed(ob ject sender, ElapsedEventArg s e)
| {
| mutex.waitone() ;
| StreamWriter swLog = new StreamWriter(pa th, true);
| swLog.AutoFlush = true;
| swLog.WriteLine (DateTime.Now.T oString() + ": " + message);
| swLog.Close();
| mutex.ReleaseMu tex();
| }
| }
|
| 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.Threadin g;
using System.Collecti ons;
using System.Collecti ons.Generic;
class Program
{
private static EventWaitHandle evntWh;
static void Main()
{
Program prog = new Program();
LoggerQueue<str ing> logMessageQueue = new LoggerQueue<str ing>();
evntWh = new EventWaitHandle (true, EventResetMode. ManualReset);
// start a consumer
Thread loggerThread = new Thread((ThreadS tart) delegate {
prog.Consume(lo gMessageQueue); });
loggerThread.St art();
MyObject obj1 = new MyObject(logMes sageQueue);
MyObject obj2 = new MyObject(logMes sageQueue);
MyObject obj3 = new MyObject(logMes sageQueue);
MyObject obj4 = new MyObject(logMes sageQueue);
MyObject obj5 = new MyObject(logMes sageQueue);
MyObject obj6 = new MyObject(logMes sageQueue);
Console.ReadLin e();
// Request Consumer thread to terminate
evntWh.Reset();
loggerThread.Jo in();
}

private void Consume(LoggerQ ueue<string> q) {
using (StreamWriter swLog = new StreamWriter("L ogFile.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_numMessagesPo sted;
LoggerQueue<str ing> q;
System.Timers.T imer timer;
public MyObject(Logger Queue<string> logMessageQueue ) {
q = logMessageQueue ;
Random r = new Random((int) DateTime.Now.Ti cks);
timer = new System.Timers.T imer((double)r. Next(100, 2000));
timer.Elapsed += new System.Timers.E lapsedEventHand ler(Produce);
timer.Start();
}
private void Produce(object sender, System.Timers.E lapsedEventArgs e) {
// enqueue a simple string message
string s = String.Format(" Logmessage {0} threadId {1} - {2}",
Interlocked.Inc rement(ref m_numMessagesPo sted),
Thread.CurrentT hread.ManagedTh readId, DateTime.Now.To String());
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.Wai t(_queue, 1000))
return default(T);
}
_count--;
return _queue.Dequeue( );
}
}
public void Enqueue(T data)
{
if (data == null) throw new ArgumentNullExc eption("data");
lock (_queue)
{
_queue.Enqueue( data);
_count++;
Monitor.PulseAl l(_queue);
}
}
}
Willy.
Jun 1 '06 #7

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

Similar topics

1
2137
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 for each of these classes "log.xml" Now I want all the classes to log to the same file. However, this does not happen. Each class creates its own log file. The base class uses the file "log.xml". Each subsequent class creates a log file...
2
4050
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 I am not fully convinced that this problem cannot be solved. I am going to propose a solution here. For the sake of discussion I will post my solution here. It is possible that the proposed solution does not work, feedback and comments are...
0
3906
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" a command-file for new commands. If a new commands arrives a split of the workload to threads is under- taken that "control themselves" via Semaphore and are joined finally. It works.
4
1933
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 .NET has an equivalent ? Thanks in advance and regards Navin
28
2755
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 (rlog_ostream) with such a custom streambuf. rlog is now a macro defined as #define rlog (rlog_ostream<<"<"__FUNCTION__">") so it'll log the caller's function name automatically.
6
7326
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 possibility of shutting logging down at compile time and suffer no performance penalty whatsoever for getting logging on whenever I wish. Of course that I would need to recompile the project each time I want to turn logging on or off. But given a...
16
2181
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 more experienced loggers can provide me with some ideas as to how to log in a simple yet flexible manner. For instance, I'd like the code to be as uncluttered as possible by Trace statements. As an example of basic logging functionality, I've come...
3
4300
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 the logging module's facilities. The logging module itself is "thread-safe", but I think that just means that individual output is protected. If I have, in temporarly sequence:
2
4060
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; log.stream(INFO) << "this is an info message" << std::endl;
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.