|
Hi,
I have used CRITICAL_SECTION to make my code thread safe and I was wondering if there is some way of testing it. I can call the code from two different threads and the code doesnt appear to screw up - but how do I really know that the cricial sction has blocked one thread until the other has completed rather than just being (un)lucky and both threads never actually called at the same time?
Is there something I can put in there to record that two threads have attempted to access the same resource?
TIA
Tony
|
|
|
|
|
Do you always test things like that ? Did you check if the if/else statments were doing their job, or if CreateThread is really creating a new thread ?
Seriously, if you use them like it is documented, they will work how it is documented. No need to be paranoid .
|
|
|
|
|
Hi,
Thanks for the reply.
Yes I do like to test stuff like that - I try to test all my code, including whether my if/else's do what I expect them to do. Not that I don't trust the compiler, but I sometimes write stuff that doesn't work - maybe its just me
So if I understand your correctly, if I 'think' I understand the documentation and it 'appears' to work - then it must be OK - non need to test it right?
I just wnated to make sure that my code is robust, no corruptions - no deadlocking - that kind of stuff. Then I can sleep at night.
Cheers
Tony
-- modified at 9:38 Thursday 27th July, 2006
|
|
|
|
|
You sqid this:
softwaremonkey wrote: but how do I really know that the cricial sction has blocked one thread until the other has completed rather than just being (un)lucky and both threads never actually called at the same time?
I understood that you wanted to check if the critical section were working properly, not your code. In that case, I think it is a litle bit useless to verify things that are used by milion of people
But I understand now (yes, I know, my brain is slow today ) that you want to check your code. Something that comes to my mind: in your thread, you can simply make a very lenghty operation (like a double for loop) that is inside a locked section. Then, with the other thread, wait that the critical section is freed. Log the times in a file to see if the second thread is really waiting for the critical section. Of course this is a basic scenario but you can improve it (e.g. use a lenghty operation in both threads)
|
|
|
|
|
Hi,
Thanks, I will try that.
Incidentally, I came across this intersting article about testing critical sections...
http://msdn.microsoft.com/msdnmag/issues/03/12/CriticalSections/default.aspx
It also has some code for some kind of debugging tools. I havent checked it out yet - still reading the article.
Thanks again for the help.
Tony
|
|
|
|
|
A simpler test:
have a volatile bool variable with global lifetime, visible in the critical section of code.
Initialize to zero before you start the thread(s).
Directly after Lock, check reentrancyFlag == 0
set flag to 1
Upon exit, set to 0
Make sure that you never forget to reset to zero even if you exit early through return or exception (e.g. through a scopeguard[^])
For a very critical test, put a Sleep() into the protected block 8debug build only, of course), so that overlaps are much more likely.
|
|
|
|
|
If you are running under a debugger, you could always manipulate two threads so that one gets into the critical section and waits (In VS .NET 2003, Right click on Threads Pane and click Freeze) , while you switch to the second thread. Then, you can allow the second thread to run. You should be able to know immediately whether the second thread blocks.
|
|
|
|
|
Thanks for the replies guys,
If anyone is interested, I have also found that you can check the LockCount member of CRITICAL_SECTION to see if a thread is already in the critical section.
LockCount == -1 if the critical section is not already in use e.g.
if(lock.LockCount >= 0)
{
TRACE("Locked\n");
}
Tony
|
|
|
|
|
Hi All
I have to create a GUI in turbo C++ for Dos.
and I want to give Windows like look in interface.
so can you please tell me any library for GUI which could be used in DOS.
or tell me any better approach.
Thanks
Edit/Delete Message
|
|
|
|
|
Use Borland C++ 2.0 (or maybe 3.0). It comes with TurboVision. I believe 3.0 could produce oth Windows and DOS apps.... it's been over 15 years since I did a DOS app...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Humm, this seems Hard to do with TurboC++.
See here for some help[^]
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
rajeevktripathi wrote: I have to create a GUI in turbo C++ for Dos.
and I want to give Windows like look in interface.
Never thought i'd hear this
rajeevktripathi wrote: or tell me any better approach.
Use Windows
Help your local Search & Rescue. Get lost!
|
|
|
|
|
Monty2 wrote: Use Windows
He's already stated that he needs to write a DOS app, so it's obviously a *requirement* over which he has no control. People that come here and state their requirements aren't typically in the mood for off-the-cuff bullshit answers like yours.
-- modified at 9:54 Thursday 27th July, 2006
|
|
|
|
|
Thanks U all
OK I will start to write such programs
can u please guide me how to proceed
But would like to tell the reason whiy I need ..
Infact working on such project where if system is crashed then system will be started with a bootable floppy ( in DOS mode) and our application will be invoked so wanna write GUI code for that part..
Please help me
Thanks
|
|
|
|
|
Telling you how to write an application is WAY beyond the scope of this forum.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Isn't that called Windows 3.0?
More seriously, when I did DOS programming, there were several graphics libraries, UI toolkits and other such things available. Unfortunately, not only are the obsolete, most the companies don't exist any more. (I last evaluated one of these libraries in 1989--the name "GreenLeaf" pops into mind--if they are who I remember them to be, contact them and see if they have some archived stuff they can sell you.)
A big problem is finding products that actually have drivers for DOS (this was the big bugaboo when doing this; you had to ship with a ton of graphics drivers and hoped that the user knew which one they were using since there was no standard detection method [somewhere, I have the code to detect if you were running Hercules mono, CGA, EGA or VGA graphics.])
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Unfortunately, modern support for the 'old' video standards is hit-or-miss. I've got an old text-mode DOS app that my company uses in manufacturing. We've got a shrinking pile of old hardware that gets swapped around and periodically refurbished for the app to run on. I've not had good luck running it on contemporary hardware. BIOS functions are missing or incorrectly implemented, hardware register emulation is half-assed, and so on. I get the impression that, about ten years ago, someone wrote a 'text-mode emulator' package in VHDL, and everyone's been using it since then without ever testing it.
Yeah, I know it would be smarter for me to rewrite the app. It's a lot cheaper, however, to have a technician scrounge up some hardware every six months and spend half a day putting it together.
Software Zen: delete this;
|
|
|
|
|
I need to get a list of all the Bluetooth Devices that the PC is paired with.
We make a Bluetooth device that my code talks to as though it were a COM Port. All the paired devices appear in a list in 'Bluetooth Devices'. At present my user has to pick a com port from a list of 1 to 256. It would be alot better to tell them what device is connected to which COM port and let them pick from that list.
I haven't a clue where to look, and Googling gives me loads of results, almost all of which are at a much less detailed level.
Any ideas anyone?
Ali
|
|
|
|
|
Does anyone know how to communicate with a web server through 2-way SSL (https) from an MFC app using the clients default browsers certificate store? I have to make an app work that uses a variety of browsers (Netscape 7.2, IE, and Mozilla Firefox) certificate stores to communicate with a WebLogic server.
Any help would be greatly appreciated.
Stuart
|
|
|
|
|
how to concatenate strings with longs for example if i want to do that:
long j=5;
MessageBox("the number is"+J+"apples");
|
|
|
|
|
You can use printf for that:
char szBuffer[50];
sprintf(szBuffer,"the number is %i apples",J);
Take a look at sprintf[^] and format spec[^] in the MSDN for more details.
|
|
|
|
|
See CString::Format and wsprintf(...)
|
|
|
|
|
Also you can use CString class as,
CString str("");
LONG l = 5;
str.Format("The Number is %ld Apples", l);
MessageBox(str);
Do your Duty and Don't expect the Result
|
|
|
|
|
I have a SDI program that uses ScroolView. If I put only 25 lines of text everything it's ok but if I put out 250 lines then it won't display it all. I know I can set the scroolsize with SetScrollSizes in the OnInitialUpdate() function. My question is : can I modify that scrool size throughout the program?
|
|
|
|
|
tanarnelinistit wrote: My question is : can I modify that scrool size throughout the program?
Yes of course.
BTW, why didn't you test it ? You just have to add two or three lines of code and you'll get your answer
|
|
|
|