|
Dear Cédric Moonen:
Good job. You are right. I write a test program in window XP, it actually spends about 15 milliseconds when context-switch occurs.
Thank you very much!
------
UINT beginTicks,testData=0,MsTimePerLoop;
while(1)
{
beginTicks=GetTickCount();
for(int i=0;i<10000;i++)
testData= (testData+100)*20-300*20+20;
MsTimePerLoop=GetTickCount()-beginTicks;
if(MsTimePerLoop>=5)
{
printf("MsTimePerLoop=%d\n",MsTimePerLoop);
Sleep(1000);
}
else
printf("MsTimePerLoop=%d\r\n",MsTimePerLoop);
}
|
|
|
|
|
yctsai wrote: The context switch doesn't spend 15 MS (too long).
Maybe not, but the context switch PLUS the time slice given to a different process will easily take 15ms. Remember, you're not the only code running on your PC.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
It may be that your thread is preempted as Cédric suggested.
But, it may also be that your call to ::ReadFile() doesn't return immediately due to how you have set up your port and whether you're using overlapped I/O or not.
So; I have two questions:
- What values have you assigned the members of the
COMMTIMEOUTS struct when calling ::SetCommTimeouts() ?
This is essential since your call may block until it receives something on the port. - Are you using overlapped I/O?
My point is that the delay you're experiencing may be caused by the device connected to your port as well as any context switches.
I also suggest you have a look at this article[^] about serial communication.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
While you can do nothing about the 15ms delay, see if this article is of any help with serial ports in general.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hello everyone,
i am trying to compile my program where i am using the functions like sqrt pow and fabs. I do have math.h included but for some reason i get errors like:
error C2668: 'fabs' : ambiguous call to overloaded function
same for the rest of the functions
i have this included:
#include "stdafx.h"
#include "math.h"
i tried including <cmath> but still same errors.
Does anyone know why they are not being recognized?
my file is .cpp not .c but it is an MFC project.
thanks
|
|
|
|
|
It's because those functions are overloaded for several types: float, double and long double. Thus, if you pass in an integer, the compiler doesn't which one to choose. An easy fix is to simply pass a double (multiply what you pass in by 1.0), this should fix the problem.
int result = (int)fabs(value*1.0);
|
|
|
|
|
Cedric Moonen wrote: int result = (int)fabs(value*1.0);
Better could be
int result = (int)fabs((double)value);
Because it avoids a multiplication. Am I right?
- ns ami -
|
|
|
|
|
Yes you're right. It's just because I am used to multiply by 1.0 because in general I use that in calculation (where I don't want to put a cast inside because it looks ugly)
|
|
|
|
|
ns ami wrote: Am I right?
Only if the code were in a huge loop would the difference in time even be noticeable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
it won't matter at all. the multiplication will be optimized away, and the two different calls will end with exactly the same object code.
|
|
|
|
|
nevertheless it's a bad style as many people won't understand it.
Don't try it, just do it!
|
|
|
|
|
the compiler will optimize that multiplication away.
int value = rand();
int result1 = (int)fabs(value*1.0);
printf("%d, %d\n", result1, result1);
VC6, release mode gives the following assembly:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs(value*1.0);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
no fmul.
if you do this:
int value = rand();
int result1 = (int)fabs((double)value);
printf("%d, %d\n", result1, result1);
you get this:
; 10 : int value = rand();
call _rand
; 11 : int result1 = (int)fabs((double)value);
mov DWORD PTR -4+[esp+4], eax
fild DWORD PTR -4+[esp+4]
fabs
call __ftol
; 12 : printf("%d, %d\n", result1, result1);
push eax
push eax
push OFFSET FLAT:??_C@_07JPPP@?$CFd?0?5?$CFd?6?$AA@ ; `string'
call _printf
exactly the same.
|
|
|
|
|
Chris Losinger wrote: the compiler will optimize that multiplication away.
Thank you for pointing out that...
- ns ami -
|
|
|
|
|
i need alternative code for CMap is it possible to implement my own CMap wrapper Class using the STL::map
Hi this Ravinder
|
|
|
|
|
I don't really understand your question but a std::map is an alternative for the MFC CMap (in fact, I much prefer the STL containers than the MFC containers).
|
|
|
|
|
... in addition, perhaps the biggest difference is that CMap uses hashing for its keys, and std::map uses key comparisons to store them in some sorted order.
--
Arman
|
|
|
|
|
Need the wrapper class for CMap using the STL map(Because i have some problems using CMap i want the same functionality of CMap but internally i have to use map)
Hi this Ravinder
|
|
|
|
|
So you need the interface of CMap and the implementation of std::map..? If so, then you need to declare your own CMyMap with an std::map member variable declared in it, and try to implement CMap similar functions in your CMyMap. However, you cannot simulate hash code related functions of CMap via a std::map..
Did I understand your problem correctly?
--
Arman
|
|
|
|
|
Hi All,
How to get the available ports for serial port communcation.
Thanks & Regards,
Anitha
|
|
|
|
|
You can use CP article Search engine..
See here[^]. I hope it helps.
Regards,
Sandip.
|
|
|
|
|
Check out the Win32_SerialPortConfiguration and Win32_SerialPort WMI classes.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi all,
I am just curious to know if there is a way to find the HWND of a window, given no actual window title and no definitive process name. Although I could get a handle by using the process name, I'd rather not since users might rename the executable file to something else.
I am attempting to prevent two instances of one app from being run at once, so I stuck a mutex in. I want my app to send a message to the already running program and close. However, my app needs the HWND of the other running app to send a message to it with SendMessage. The problem is, my app does not have an actual "window", meaning that I can't get it's HWND with FindWindow. It's derived from CFrameWnd and doesn't show up in taskmgr (FindWindow).
Any suggestions on how to accomplish this? Thanks in advance
|
|
|
|
|
How about saving the HWND into a registry location?
«_Superman_»
|
|
|
|
|
That'd actually be a good idea.
I'll try that. But is there any more...elegant..way to do this?
|
|
|
|
|
What about using events..??
Regards,
Sandip.
|
|
|
|