Threading singleton class?

B

Bryan

Im new to threading and am experiencing some confusion as to how to
correctly use AfxBeginThread in my singleton class. The code that I
want to thread out is in a single function, Init(). Is AfxBeginThread
the right way to thread off a single worker thread? It seems like it.

But what are the correct args for it, my code wont compile. And does my
class need to be restructured? How?

Thanks,
Bryan

I have this:
class CHandler
{
public:
~CHandler(void);
static CHandler& Instance();
bool GetInitialized() { return m_initialized; }

private:
CHandler(void);
CHandler(const CHandler&);
CHandler& operator=(const CHandler&);

void Init(LPVOID pParam);
bool m_initialized;
};

CHandler::CHandler(void)
{
Init();
}

CHandler::~CHandler(void)
{
// clean up code
}

CHandler& CHandler::Instance()
{
static CHandler instance;

// This is something like what I want to add to thread Init()
//if (!instance.GetInitialized()) {
// AfxBeginThread(Init, &instance);
//}
return instance;
}

void CHandler::Init(LPVOID pParam)
{
// I dont need pParam here (I think) do I have to have it?
try {
// Specific call to other code that I want to thread!
// This stuff takes forever and only can happen 1x!
if (!mclInitializeApplication(NULL,0))
{
throw std::exception("");
}
if (!clusterlibInitialize())
{
throw std::exception("");
}
m_initialized = true;
}
catch(...) {
m_initialized = false;
}
}
 
R

red floyd

Bryan said:
Im new to threading and am experiencing some confusion as to how to
correctly use AfxBeginThread in my singleton class. The code that I
want to thread out is in a single function, Init(). Is AfxBeginThread
the right way to thread off a single worker thread? It seems like it.

Wrong group. Try microsoft.public.vc.mfc
 
V

Victor Bazarov

Bryan said:
Im new to threading and am experiencing some confusion as to how to
correctly use AfxBeginThread in my singleton class. [..]

Threading is not part of the language (yet, at least), so much of what
can be discussed about threading falls outside the scope of c.l.c++.
AfxBeginThread is not a standard C++ function. Both those reasons let
me believe you're in a wrong newsgroup. See FAQ for the list of the
suggested alternatives.

V
 
S

Steve Pope

Victor Bazarov said:
Bryan wrote:
Im new to threading and am experiencing some confusion as to how to
correctly use AfxBeginThread in my singleton class. [..]
Threading is not part of the language (yet, at least), so much of what
can be discussed about threading falls outside the scope of c.l.c++.

I'm not sure if I agree with this logic. Typically algorithms and
programming techniques that are "not part of the language" are
still valid topics for discussion here. Threading is just another
algorithm/technique isn't it?

Steve
 
V

Victor Bazarov

Steve said:
Victor Bazarov said:
Bryan wrote:
Im new to threading and am experiencing some confusion as to how to
correctly use AfxBeginThread in my singleton class. [..]
Threading is not part of the language (yet, at least), so much of
what can be discussed about threading falls outside the scope of
c.l.c++.

I'm not sure if I agree with this logic. Typically algorithms and
programming techniques that are "not part of the language" are
still valid topics for discussion here. Threading is just another
algorithm/technique isn't it?

Not language-related technique or algorithm. And any problems that
arise in programming threads cannot be answered in terms of C++ alone
since C++ does not define them. There is c.p.th for that.

Discussions on development of language elements needed to introduce
threading in C++ are fine. Vice versa usually not.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top