Trash Talk - C/C++ Thingies. ExplodyThingy - Sat May 01, 2004 8:55 pm Post subject: C/C++ Thingies.
Fantastic. Hit something I should have learned, but didnt since I havent had a class in C/C++. Im trying to create a single instance of a class across the entire program. It would be the CFG class used to maintain the variables so I dont have to do GetPrivateProfileString every time, or create several instances of it which would seriously screw up a refresh of the class. I know its via "extern" by I haven't been able to get the syntax and setup correct. Im not sure if its a problem with something like this, but the program is multithreaded.
Mr Ekted - Sat May 01, 2004 8:57 pm Post subject:
You are trying to make it so all files see the same piece of data (in the case a class)?
File 1:
MyClass xyz;
File 2:
extern MyClass xyz;
File 3:
extern MyClass xyz;
It must be declared outside of any scoping mechanism (eg not in a function), making it public.
ExplodyThingy - Sat May 01, 2004 9:07 pm Post subject:
Gracies, and that was really friggin fast.
ExplodyThingy - Sat May 01, 2004 9:13 pm Post subject:
Ok, didnt do what I wanted it to do.
I have "extern CFG cfg;" at the bottom of CFG.h to keep it across all the files, for now. In main.cpp, the file with main(), there is "CFG cfg;". In the CFG constructor there is "printf("CFG Made.\n");" Program runs and I see that once. If I goto another file, for example threads.cpp which also includes CFG.h, and do cfg.meh(), I see it "CFG Made.\nCFG Made.\n" printed out.
Mr Ekted - Sat May 01, 2004 11:02 pm Post subject:
What does cfg.meh() do?
ExplodyThingy - Sat May 01, 2004 11:46 pm Post subject:
its just "printf("meh.\h");" The class contains a constructor and a meh() function, no instance variables at the moment. What has me worried is that I see the constructor execute twice when it first revs up.
Mr Ekted - Sun May 02, 2004 12:42 am Post subject:
Debug it. Put a breakpoint in the contructor. When it breaks, note the value of "this" (pointer to the current object). If you see 2 different values of "this" each time it is called, then somehow you have instantiated 2 different instances of the class. If not, then something is wrong in the way the class is configured. If you are still stuck, I can look at your source.
numpf - Sun May 02, 2004 8:59 am Post subject:
You aren't calling new CFG(); right? You know that the CFG cfg; declaration in main.cpp implicitly calls the constructor before main() gets called right?
The single-instance-per-process pattern is so common is has a name: Singleton. If you google it you can find how to rigorously code it (if you want), or at least read about the pitfalls of doing something like that. Make sure you find something about C++ specifically; singletons can and should be done differently in java, so reading about implementing a java singleton doesn't help you.
-numpf
Anonymous - Mon May 03, 2004 7:51 am Post subject:
Of course Im not calling new() outside the implicit one. The flaw may be that I had "extern CFG cfg;" at the bottom of cfg.h, which naturally is inluded into main.cpp. In main.cpp there is the "CFG cfg;", so it essense has "extern CFG cfg; CFG cfg;" Ill try moving it first then, doing a little more research on Singleton.
Again, multithreading wouldnt be a problem would it? At the moment, there is only 1 function ever called from the same thread as main(), and that is main() itself.
If theres still a problem Ill post the whole thing on my site.
Mr Ekted - Mon May 03, 2004 2:36 pm Post subject:
extern CFG cfg; Cfg cfg; is fine. That won't create 2 instances.
ExplodyThingy - Mon May 03, 2004 6:19 pm Post subject:
Ha, fixed. Some macro somewhere was getting set wrong so it made it twice. Stupid me. Now, is there anyone familiar with POSIX Threads that knows how to make thread-safe stuff? Such as, thread locking?
Mr Ekted - Mon May 03, 2004 7:58 pm Post subject:
What does this app do? I've had many people ask me about how to do multi-threading only to find out they were using it when they didn't need to. It's cool to play with it to learn, but it's a waste in general if it's not needed -- a little extra design, slightly more complex code and overhead.
Anonymous - Tue May 04, 2004 7:49 am Post subject:
Its a trial of a TCP/UDP billing server. You know, those knock-off things I have on my website that dont really work except that this one actually does work. For something as slow running as a billing server Im sure its more overhead than I need, but since the socket wrappers I am currently using are blocking, its the best fit. Naturally, again I found what I was looking for on the 'net with pthread_mutex'es.
And just so you know, almost every program Ive ever written has been simply to learn and play with. Thats why once I get them quasi-running, I often get bored (a la explbot).
Mr Ekted - Tue May 04, 2004 3:55 pm Post subject:
Is this app supposed to be cross-platform? If not, I suggest you make it using raw non-blocking socket and a single thread.
ExplodyThingy - Tue May 04, 2004 4:03 pm Post subject:
Geeze. Just reply immediately after I reply to your PM why dont you.
It doesnt need to be cross platform, but it doesnt hurt either. Te only real reason Im using blocing sockets is because I dont have a good multi-session nonblocking TCP server side wrapper and dont yet have the know how to do my own.