Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static member variable in multithreaded environment

Reply
Thread Tools

static member variable in multithreaded environment

 
 
nin234@yahoo.com
Guest
Posts: n/a
 
      02-23-2005
I tried searching the newgroups for an answer to this question. I
found
many related topics, but didn't find the exact answer I am looking
for.


I am implementing a class to aid in the logging for our product as
follows. Code snippet is as follows.

//Header file srvLog.h starts here
enum eErrLvl
{
eLOG_DEBUG_5 = 1,
eLOG_DEBUG_4,
eLOG_DEBUG_3,
eLOG_DEBUG_2,
eLOG_DEBUG_1,
eLOG_INFO,
eLOG_WARNING,
eLOG_ERROR,
eLOG_FATAL
};

std:stream& operator << (std:stream&, eErrLvl&);

class srvLog
{
static int nLglvl;
eErrLvl nSeverity;
public:
srvLog () {}
static void setlvl (int lvl) { nLglvl = lvl; }
const srvLog& operator () (eErrLvl nSvrty) { nSeverity =
nSvrty; return *this;}
friend std:stream& operator << (std:stream& , const
srvLog&);
};

//Source file srvLog.C starts here
int srvLog::nLglvl = eLOG_ERROR;

std:stream&
operator << (std:stream& os, eErrLvl& eLvl)
{
switch (eLvl)
{
case eLOG_DEBUG_5:
case eLOG_DEBUG_4:
case eLOG_DEBUG_3:
case eLOG_DEBUG_2:
case eLOG_DEBUG_1:
os << "Debug\t: ";
break;
case eLOG_INFO:
os << "Info\t: ";
break;
case eLOG_WARNING:
os << "Warn\t: ";
break;
case eLOG_ERROR:
os << "Error\t: ";
break;
case eLOG_FATAL:
os << "Fatal\t: ";
break;
}
return os;
}
std:stream& operator << (std:stream& os, const srvLog& oLog)
{
if (oLog.nSeverity >= oLog::nLglvl)
os.clear();
else
{
os.clear (std::ios::failbit);
return os;
}
char msg[100];
//memset (msg, '\0', 100);
struct timeval now;
gettimeofday (&now, NULL);

struct tm *pTm = localtime (&now.tv_sec);
strftime (msg, 100, "%c ", pTm);
os << msg << oLog.nSeverity;
return os;
}


//Sample use case

int main ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
pthread_create (, threadA, ... , ...);
pthread_create (, threadB, ... , ...);

}
//ThreadA in a different file and shared library
void threadA ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::end;
}

//ThreadB in a different file and shared library
void threadB ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
}

Logging is controlled by the nLglvl static variable. This is part of
a daemon. So the logging is to a file and the value of nLglvl will be
set by a command line tool.
My questions are the following
1) Is the value of nLglvl shared by threadA, threadB and main()
thread.

2)Where should I call setlvl()?
3) If the answer to 1) is yes. Is it really required to use locking
in
this scenario. Because the change to the value of nLglvl will be
infrequent and I can live with some bad reads.

I am using POSIX threads. Linux and solaris are the current
environments. It must be portable to all unix flavors. I threads are
not part of the C++ standard, but to me it does n't make much sense. I
feel this is as much a C++ question as a Threads one

Ninan

 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-23-2005
nin234 wrote:

> I tried searching the newgroups for an answer to this question.
> I found many related topics
> but didn't find the exact answer I am looking for.
>
>
> I am implementing a class
> to aid in the logging for our product as follows.
> Code snippet is as follows.


> cat srvLog.h

#ifndef GUARD_SRVLOG_H
#define GUARD_SRVLOG_H 1

#include <iostream>

//Header file srvLog.h starts here
enum eErrLvl {
eLOG_DEBUG_5 = 1,
eLOG_DEBUG_4,
eLOG_DEBUG_3,
eLOG_DEBUG_2,
eLOG_DEBUG_1,
eLOG_INFO,
eLOG_WARNING,
eLOG_ERROR,
eLOG_FATAL
};

std:stream& operator<<(std:stream&, const eErrLvl&);

class srvLog {
private:
static int nLglvl;
eErrLvl nSeverity;
public:
srvLog(void) { }
static void setlvl(int lvl) { nLglvl = lvl; }
const srvLog& operator()(eErrLvl nSvrty) {
nSeverity = nSvrty;
return *this;
}
friend
std:stream& operator<<(std:stream&, const srvLog&);
};

class A {
public:
friend
std:stream& operator<<(std:stream& os, const A& a) {
return os << 13;
}
};

#endif//GUARD_SRVLOG_H

> cat srvLog.C

//Source file srvLog.C starts here
#include <sys/time.h>
#include "srvLog.h"

int srvLog::nLglvl = eLOG_ERROR;

std:stream&
operator<<(std:stream& os, const eErrLvl& eLvl) {
switch (eLvl) {
case eLOG_DEBUG_5:
case eLOG_DEBUG_4:
case eLOG_DEBUG_3:
case eLOG_DEBUG_2:
case eLOG_DEBUG_1:
os << "Debug\t: ";
break;
case eLOG_INFO:
os << "Info\t: ";
break;
case eLOG_WARNING:
os << "Warn\t: ";
break;
case eLOG_ERROR:
os << "Error\t: ";
break;
case eLOG_FATAL:
os << "Fatal\t: ";
break;
}
return os;
}

std:stream&
operator<<(std:stream& os, const srvLog& oLog) {
if (oLog.nSeverity >= srvLog::nLglvl) {
os.clear();
char msg[100];
//memset (msg, '\0', 100);
struct timeval now;
gettimeofday(&now, NULL);

struct tm* pTm = localtime (&now.tv_sec);
strftime (msg, 100, "%c ", pTm);
os << msg << oLog.nSeverity;
}
else {
os.clear (std::ios::failbit);
}
return os;
}

> cat main.C

//Sample use case
#include "srvLog.h"
#include <pthread.h>

int main(int argc, char* argv[]) {
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log "
<< ainst << std::endl;
// pthread_create(, threadA, ... , ...);
// pthread_create(, threadB, ... , ...);
return 0;
}

//ThreadA in a different file and shared library
void threadA(void) {
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log "
<< ainst << std::endl;
}

//ThreadB in a different file and shared library
void threadB(void) {
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log "
<< ainst << std::endl;
}

> g++ -Wall -ansi -pedantic -o main main.C srvLog.C
> ./main

Wed Feb 23 09:10:23 2005 Fatal : more to log 13

> Logging is controlled by the nLglvl static variable.
> This is part of a daemon so the logging is to a file
> and the value of nLglvl will be set by a command line tool.
>
> My questions are the following
>
> 1) Is the value of nLglvl shared
> by threadA, threadB and main() thread.


nLglvl is just a global variable in the srvLog namespace.

> 2) Where should I call setlvl()?


You should rewrite setlvl(int) so that srvLog::nLglvl
is protected by mutual exclusion.
Then you can call it anywhere at any time.

> 3) If the answer to 1) is yes.
> Is it really required to use locking in this scenario?


Practically speaking, yes.

> Because the change to the value of nLglvl will be
> infrequent and I can live with some bad reads.


You say that now ...

> I am using POSIX threads.
> Linux and Solaris are the current environments.
> It must be portable to all UNIX flavors.
> I [know that] threads are not part of the C++ standard
> but, to me, it doesn't make much sense.
> I feel this is as much a C++ question as it is a threads question.


The only C++ question is about the static (global) variable nLglvl.
Now, you would be *much* better off to post your other questions
(about where to call srvLog::setlvl(int), portability, etc.)
in one of the UNIX newsgroups.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
Does a static variable in a class's member fn always remain static? Sam C++ 4 01-13-2004 11:05 PM
Database & 'autonumber' in multithreaded environment Joerg Gippert Java 9 10-10-2003 08:13 PM
iostreams in multithreaded environment Anthony C++ 3 10-08-2003 06:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57