473,394 Members | 1,810 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,394 software developers and data experts.

Global vs local streams in a multithreaded app

I'm maintaining a C++ application written by a developer who has now
left. The app is a multithreaded client/server app for financial data.
My compiler is MS visual C++ 6.0.

I'm a C++ neophyte. I took some C (not C++) courses in college, this is
the only production C++ code I've worked with.

I'm trying to add some logging, and having problems.

My logging scheme is to have a global ofstream, which I initialize in a
function by creating a local ofstream, preparing it, and assigning it
to the global name.

The problem is that I can << to the local name without trouble, but I
get an access violation when I try to << to the global stream (after
assigning global = local).

I think the problem may be due to the multithreaded nature of the
application, but am unsure. At the moment, I'm only <<ing to the log
in one function, in the main (server) thread of the app. There are no
multiple concurrent accesses to the global log.

This code demonstrates the nub of the problem:

-----Globals.h-------

#include <fstream>

class GLOBALS
{
public:
ofstream GlobalLog;
}

------SomeOtherFile.cpp-------
#include "Globals.h"

extern GLOBALS * g; // This is populated properly, I just don't show
that here.

void foo()
{
if(!g->GlobalLog.is_open())
{
ofstream localNameForTheLog("c:\\thelogIwant.log" , ios::out |
ios::ate | ios::app);

if(!localNameForTheLog.is_open())
{
cout << "Couldn't open log.";
return;
}

localNameForTheLog << "Some stuff I'm able to log successfully";

g->GlobalLog = localNameForTheLog;

}

// If we got here, either GlobalLog was already open,
// or we just successfully opened it.
g->GlobalLog << "This causes an access violation for some reason, even
though I just logged the line above.";

}

Jul 23 '05 #1
1 2317
da*********@gmail.com wrote:

g->GlobalLog = localNameForTheLog;


iostreams aren't assignable. This line only "works" because there is a
secret assignment operator that's used during initialization. It doesn't
do a proper assignment, though.

But there's no need to go through all this indirection. Just open the
global stream using ofstream::open.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

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

Similar topics

35
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
13
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
3
by: groups | last post by:
Hi all, I've recently ported a rather large C application to run multithreaded. A few functions have seriously deteriorated in performance, in particular when accessing a rather large global...
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
1
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
2
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
2
by: year1943 | last post by:
There was the same topic not so long ago, but as I see it stays w/o answer:...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.