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

Threading singleton class?

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;
}
}
Dec 12 '06 #1
4 1454
Bryan wrote:
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
Dec 12 '06 #2
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++.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 12 '06 #3
Victor Bazarov <v.********@comAcast.netwrote:
>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
Dec 13 '06 #4
Steve Pope wrote:
Victor Bazarov <v.********@comAcast.netwrote:
>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 13 '06 #5

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

Similar topics

0
by: Zuel | last post by:
Sup everyone! I wrote this code for Tomcat appserver but I am told from an associate that it has threading issues. The basic idea is to store a read only data table for everyone to use. It...
2
by: Andrea Manzini | last post by:
Hi all, I'm a python newbie with a little problem; my need is to share an incrementing counter between threads; i.e. every time I create a new thread (which handle a single tcp/ip connection), I...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
5
by: Roshan | last post by:
This is regarding the article titled "C++ & Double-Checked Locking" by Scott Meyers and Andrei in DDJ July 2004 issue. I think the reasoning in this article is fundamentally flawed due the...
10
by: muscha | last post by:
Hi All, I'm having a bit of problem with threading, when I first call Start() on my code the thread work, I call Stop() and it stopped, but when I call Start() again my thread is not started....
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
2
by: greg.merideth | last post by:
I have a project where we have a windows service that creates a remoting object for an external client application to communicate with using ipc. We've discovered the client is making updates to...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.