Hi,
As per the documentation.each thread has its own ACE_Log_Msg instance.On debugging I found that sometimes two threads have the same ACE_Log_Msg instance.Bcoz of this inconsistency my application crashes .
Please let me know whats the actual prob.

So have you determined whether this is the cause or an effect?

Do the threads start off with different instances, but through some other bug in your code they become the same and at that point it crashes.

Thanks Salem for your response.
I have written a simple program just to test how ACE_Log_Msg works.
Here is a class named LogManger which directs Logging output to Ostream or STDERR or both.
The Main thread calls the function "redirectToFileWithTimeStamp" of this class and also creates a child thread which simply prints any arbitrary message.
The application crashes only when Both the threads have the same instance of ACE_Log_Msg.
Below is the code snippet for your reference.

class LogManager 
{
public
: 
LogManager ();
~LogManager ();
void redirectToFile(const char *filename, ACE_TCHAR* ptr); 
void redirectToOStream (ACE_OSTREAM_TYPE *output,ACE_TCHAR* ptr); 
private
: 
ofstream* log_stream_;
ACE_OSTREAM_TYPE* output_stream_;
};
typedef
ACE_Singleton<LogManager, ACE_Null_Mutex> LogManagerSingleton; 
 
// here is LogManager.cpp
void
LogManager::redirectToFile (const char *filename, ACE_TCHAR* ptr) 
{
log_stream_ = 
new std::ofstream (filename, ios::out | ios::app); 
this->redirectToOStream (log_stream_,ptr); 
} 
void
LogManager::redirectToOStream (ACE_OSTREAM_TYPE *output,ACE_TCHAR* data) 
{
ACE_Log_Msg* ptr = ACE_LOG_MSG->instance(); 
 std::ostream* stream = ACE_LOG_MSG->msg_ostream();
ACE_LOG_MSG->msg_ostream(output); 
u_long orig_flags = ACE_LOG_MSG->flags();
if((orig_flags & ACE_Log_Msg::OSTREAM) != ACE_Log_Msg::OSTREAM) 
{ 
ACE_LOG_MSG->clr_flags(orig_flags);
ACE_LOG_MSG->set_flags(ACE_Log_Msg::OSTREAM);
ACE_DEBUG((LM_DEBUG, 
"%s", data)); //write the message to file 
ACE_LOG_MSG->clr_flags(ACE_Log_Msg::OSTREAM); 
ACE_LOG_MSG->set_flags(orig_flags); 
//set the original flags 
ACE_LOG_MSG->msg_ostream(stream); 
}
else 
{
ACE_LOG_MSG->msg_ostream(stream); 
ACE_DEBUG((LM_DEBUG, ACE_TEXT(data))); 
//write the message to file 
}
}
// Here is the main
static
int worker(void *arg) 
{
ACE_Log_Msg* ptr = ACE_LOG_MSG->instance();  // here i found that both the threads have same ACE_Log_Msg instance
ACE_DEBUG((LM_DEBUG,"Hello\n")); // here is the crashing point 
return 0; 
}
int
main(int argc, char *argv[]) 
{
LOG_MANAGER->redirectToFileWithTimeStamp(
"nihar.txt","hi") ; 
int
n_threads= ACE_OS::atoi(argv[1]); 
//number of threads to spawn
ACE_thread_t *threadID = 
new ACE_thread_t[n_threads+1]; 
ACE_hthread_t *threadHandles = 
new ACE_hthread_t[n_threads+1]; 
if
(ACE_Thread_Manager::instance()->spawn_n(n_threads, 
(ACE_THR_FUNC)worker,
//Execute task one 
0, 
//No arguments 
THR_NEW_LWP, 
//New Light Weight Process 
ACE_DEFAULT_THREAD_PRIORITY,
1)==-1) 
ACE_ERROR((LM_ERROR,
"Failure to spawn first group of threads: %p \n")); 
LOG_MANAGER->redirectToFileWithTimeStamp(
"nihar.txt","hi") ; 
 
for
(int i=0; i<n_threads; i++) 
ACE_Thread::join(threadHandles[i]);
getch();
return
0; 
}

Gee, what did you indent that code with - your feet?

Sorry Salem .I just copt pasted the code from my file.Here is the code indented with hands!

class LogManager
{


public :
LogManager ();
~LogManager ();
void redirectToFile(const char *filename, ACE_TCHAR* ptr);
void redirectToOStream (ACE_OSTREAM_TYPE *output,ACE_TCHAR* ptr);


private:
ofstream* log_stream_;
ACE_OSTREAM_TYPE* output_stream_;
};


typedef ACE_Singleton<LogManager, ACE_Null_Mutex> LogManagerSingleton;


// here is LogManager.cpp
void LogManager::redirectToFile (const char *filename, ACE_TCHAR* ptr)
{
log_stream_ = new std::ofstream (filename, ios::out | ios::app);
this->redirectToOStream (log_stream_,ptr);
}


void LogManager::redirectToOStream (ACE_OSTREAM_TYPE *output,ACE_TCHAR* data)
{
ACE_Log_Msg* ptr = ACE_LOG_MSG->instance();
std::ostream* stream = ACE_LOG_MSG->msg_ostream();
ACE_LOG_MSG->msg_ostream(output);
u_long orig_flags = ACE_LOG_MSG->flags();
if((orig_flags & ACE_Log_Msg::OSTREAM) != ACE_Log_Msg::OSTREAM)
{
ACE_LOG_MSG->clr_flags(orig_flags);
ACE_LOG_MSG->set_flags(ACE_Log_Msg::OSTREAM);
ACE_DEBUG((LM_DEBUG, "%s", data)); //write the message to file
ACE_LOG_MSG->clr_flags(ACE_Log_Msg::OSTREAM);
ACE_LOG_MSG->set_flags(orig_flags); //set the original flags
ACE_LOG_MSG->msg_ostream(stream);
}
else
{
ACE_LOG_MSG->msg_ostream(stream);
ACE_DEBUG((LM_DEBUG, ACE_TEXT(data))); //write the message to file


}
}
// Here is the main
static int worker(void *arg) //child thread function
{
//here i found that both  the threads  have the same  ACE_Log_Msg instance
ACE_Log_Msg* ptr = ACE_LOG_MSG->instance();
ACE_DEBUG((LM_DEBUG,"Hello\n")); // here is the crashing point 
return 0;
}


int main(int argc, char *argv[])
{
LOG_MANAGER->redirectToFileWithTimeStamp("nihar.txt","hi") ;
int n_threads= ACE_OS::atoi(argv[1]); //number of threads to spawn


ACE_thread_t *threadID = new ACE_thread_t[n_threads+1];
ACE_hthread_t *threadHandles = new ACE_hthread_t[n_threads+1];
ACE_Thread_Manager::instance()->spawn_n(n_threads,
(ACE_THR_FUNC)worker,0,
ACE_DEFAULT_THREAD_PRIORITY,
1)==-1)


LOG_MANAGER->redirectToFileWithTimeStamp("nihar.txt","hi") ;


for(int i=0; i<n_threads; i++)
ACE_Thread::join(threadHandles);


getch();
return 0;
}

And yet, it's still a dog's breakfast.
Read the intro threads and figure out HOW TO USE [code]
[/code] TAGS.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.