473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::CHand ler(void)
{
Init();
}

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

CHandler& CHandler::Insta nce()
{
static CHandler instance;

// This is something like what I want to add to thread Init()
//if (!instance.GetI nitialized()) {
// 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 (!mclInitialize Application(NUL L,0))
{
throw std::exception( "");
}
if (!clusterlibIni tialize())
{
throw std::exception( "");
}
m_initialized = true;
}
catch(...) {
m_initialized = false;
}
}
Dec 12 '06 #1
4 1462
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.publi c.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.********@com Acast.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.********@com Acast.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
1446
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 takes some time for the table to be created. I want to store the table in a singleton and update it periodicly, basicaly when a user logs into the system. By keeping 1 table allocated I don't have to worry about memmory usage and if a user does...
2
1891
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 must assign an unique numeric ID to it, without reuse old numbers... In example: thread-1 id: 1 thread-2 id: 2 thread-3 id: 3 thread-100 id: 100 thread-n id: n
7
12471
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
5
1966
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 authors mixing up a couple of concepts (observable behavior & side effects) that are defined separately in the standard. Background: In the following statement... class Singelton {
10
1391
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. Although in the debugger I can see that it went to the Start() method and creating the thread again. Can anyone help me? This is the code: <snip>
18
3317
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 with the StaticClass.Method1 before it can use it? What if I removed static methods and made all the threads instantiate its own copy of the class? Would that remove the waiting contention?
13
1940
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 indicates that the forms timer does not operate on its own thread and will not cause a query to be sent to use the single SqlConnection while another is being executed from the handling of other events. Is that right? I just want to be 100% sure...
2
3973
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 the class fields by calling the update methods in the remoting object class and while thats not an issue I'm curious about threading with remote calls. I can't find much in the way of what threading issues can occur. If the service's thread...
3
18240
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.