|
Hi Roger,
Roger Stoltz wrote: The calling thread enter a busy wait state and if the thread is preempted it probably won't run until a time ridiculously greater than the resolution set with ::timeBeginPeriod().
Are you actually implying that several milliseconds pass between thread preemtions? This is incorrect.
I do some kernel driver development so perhaps I have a unique view of this; let me explain.
The maximum measureable time increment is stored in a link list at the kernel level. As you may be aware the kernel keeps track of all processes in an EPROCESS structure. The kernel also has available a struct associated with processes: PROCESS_INFORMATION_CLASS. The behavior and capabilities of each process can be accessed using NtQueryInformation and passing a valid buffer with sufficient size.
If you take a look at the struct SYSTEM_BASIC_INFORMATION:
typedef struct _SYSTEM_BASIC_INFORMATION<br />
{<br />
DWORD d00;
DWORD dKeMaximumIncrement;
DWORD dPageSize;
DWORD dMmNumberOfPhysicalPages;<br />
DWORD dMmLowestPhysicalPage;<br />
DWORD dMmHighestPhysicalPage;<br />
DWORD dAllocationGranularity;
PVOID pLowestUserAddress;<br />
PVOID pMmHighestUserAddress;<br />
DWORD dKeActiveProcessors;<br />
BYTE bKeNumberProcessors;<br />
BYTE bReserved01;<br />
WORD wReserved02;<br />
}<br />
SYSTEM_BASIC_INFORMATION,<br />
* PSYSTEM_BASIC_INFORMATION,<br />
**PPSYSTEM_BASIC_INFORMATION;<br />
<br />
#define SYSTEM_BASIC_INFORMATION_ \<br />
sizeof (SYSTEM_BASIC_INFORMATION)<br />
Using the DWORD value dKeMaximumIncrement you can determine the default "timer resolution" for any system.
<br />
SYSTEM_BASIC_INFORMATION sbi;<br />
...<br />
_stprintf((T("Maximum time resolution: %lu (%lu Hz)"), (TICKS_PER_SECOND / sbi.dKeMaximumIncrement) + (TICKS_PER_SECOND % sbi.dKeMaximumIncrement >= sbi.dKeMaximumIncrement >> 1 ? 1 : 0));<br />
The results on my system:
Maximum time resolution: 156250 (64 Hz)
So lets see what this means.
1 second contains 1000 milliseconds. (1000 / 64) == 15.625 milliseconds.
So what this means is if I do a Sleep(1) it will actually sleep about 15.625 milliseconds.
What I am saying is, the timeBeginPeriod() function is modifying the timer resolution at the kernel level which is associated with the process. This will affect all timers. The reason why MM timers function so much better is because they are actually callbacks. A standard WM_TIMER message will still be slowed by the MFC message loop.
Hopefully we all understand timers a bit better. If not I can go into greater detail.
Best regards,
-David Delaune
|
|
|
|
|
Hello again David
Randor wrote: Roger Stoltz wrote: The calling thread enter a busy wait state and if the thread is preempted it probably won't run until a time ridiculously greater than the resolution set with ::timeBeginPeriod().
Are you actually implying that several milliseconds pass between thread preemtions? This is incorrect.
No, that's not what I'm implying and if I did it would be incorrect.
What I meant was that ::Sleep() doesn't return unless the thread that called it is running. If the thread is preempted it won't run until it's given another timeslice which is approx. 10 msec under normal circumstances. This will happen if another thread is scheduled to run regardless of how ::timeBeginPeriod() works and what it affects. The resolution of the timer doesn't matter in this case.
If a developer desires to have a resolution close to 1 msec and in fact he gets a delay that exceeds 10 msec if the thread is preempted, I would consider it ridiculously greater than 1 msec.
If you still don't understand what I'm getting at, please read this[^] and this[^].
Slightly off topic:
To my knowledge the implementation of ::Sleep() is a "busy-wait" which means that it's continuously reading the time asking "are we there yet" until the desired time has passed.
This may have changed, but I still treat it as if it still is implemented as a busy-wait. It doesn't matter since I avoid calling ::Sleep() .
Regarding the WM_TIMER message it's even worse: it won't even make it to the message queue. The WM_TIMER message is a low priority pseudo message that will be handled when all other pending messages have been handled. This means that even if the timer set with SetTimer() has expired multiple times before being handled, the message handler for the timer message will only be called once.
--
Roger
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Hey guys,
There is a great addin here on Code Project:
AutoBuild[^]
But, the article has no directions on how to "install" the .dll addin, and I would like to use it with VS 2005.
Thanks for any help.
Patrick
|
|
|
|
|
Stick^ wrote: But, the article has no directions on how to "install" the .dll addin, and I would like to use it with VS 2005.
Thanks for any help.
Does this help?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hi All
I have the below bit of code, it seems that some users somehow are getting the below statement to return TRUE when m_message > 160
can anyone see any problems with this ?
thanks
Simon
int nBytes = lstrlen(m_message);
if (nBytes >160)
{
AfxMessageBox("You have exceeded the maximum number of\ncharacters allowed", MB_ICONSTOP);
return FALSE;
}
else
return TRUE;
|
|
|
|
|
I am not seeing where that would be possible, other than the possibility that m_message is a character buffer (not a string, right?), and there is some other overrun/underrun that is stepping on that buffer...? Or stuffing UNICODE data into a char buffer?
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
sorry should have put that m_message is CString
|
|
|
|
|
If it is a CString , use m_message.GetLength() instead - generally, if an object has a method for some function, you should use it because it may be optimized for that particular object.
Mismanagement of the CString object can also lead to corruption of its data - for example, if you have a reference to that CString someplace that is getting messed around with.
You can also ovverun/underrun heap memory (which the string object uses) by not being too careful with them. If you have any code that casts a CString object into a non-const pointer, that is a good place to start.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
si_69 wrote: m_message is CString
In that case instead of lstrlen, use m_message.GetLength();
nave
|
|
|
|
|
Hi all:
I need to write a function whose signature is:
std::string readWord(ifstream& in);
The function will read one word at a time from "in", and the definition of word is a sequence of alphabetic, apostrophe, hyphen or underscore characters. Anything else is treated as a "white space".
My approach is:
Use fgetc() to read one character at a time from "in", then test it to see if it is a valid character; this procedure will be repeated until the function reads all the characters between two "white spaces".
Any comments is welcome.
Thank you
|
|
|
|
|
an ifstream will only read up to a space anyhow, I would have thought that
string s;
in << s;
return s;
would do it. What you're saying will also work
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Feasible, yes. Optimal, perhaps not. Note that using fgetc(...) with a ifstream object may be troublesome.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
May I ask what would you do if you were trying to accomplish this task?
Thank you
|
|
|
|
|
Christan's approach might work, if you are looking to split a normal string into tokens by normal whitespace (tabs and spaces).
I would need to know more about what you are trying to do in order to offer a possibly more-optimal solution.
For example, if you are building a tokenizer, you might want to build something using (f)lex . If you are counting words in a small file, strtok(...) with a local buffer may work, if using a large file, skipping characters manually with a memory mapped file may work better, etc.
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Hi James:
"For example, if you are building a tokenizer, you might want to build something using (f)lex. If you are counting words in a small file, strtok(...) with a local buffer may work, if using a large file, skipping characters manually with a memory mapped file may work better, etc."
Your comments are way above my head. I just started learning C++ two weeks ago. Having said that, I do have some knowledge on C programming.
What I try to achieve is simply to produce a "concordance" program using C++. I need to count every word from a text file and make a record of where they are.
First of all, I need to read a word at one time from this text file in the way I described before. I am not so sure now which "string" function I am going to use in order to achieve my goal.
Thank you
|
|
|
|
|
C_Zealot wrote: Your comments are way above my head. I just started learning C++ two weeks ago.
That is OK! :P I sometimes do not understand my audience...
Using Christan's approach may be best for you because it (1) is simple and (2) it works (well enough).
I am not sure how his example will handle punctuation, so be aware. In other words, I do not know if the following string:
This is a simple-string, I think... Will return 6 items:
This is a simple-string, I think... -Or 12 items:
This is a simple - string , I think . . . -Or some value in between. You will need to experiment to make sure it is working correctly for your needs. I am guessing that it will only react to spaces and return 6, and I think that behavior can be changed, but always best to make sure...
Good luck!
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
C_Zealot wrote: Your comments are way above my head. I just started learning C++ two weeks ago. Having said that, I do have some knowledge on C programming.
Then stick with what you have, whether it is optimal or not. Making code optimal and efficient will come with time and experience. Get you logic down and make it understandable.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
#include < iostream >
#include < string >
using namespace std;
ifstream in("myfile.txt", ios::in);
if(in.good())
{
string strWord;
while(!in.eof())
{
strWord << in;
}
}
Something like that should work for what you want to do. You can even change the ifstream to split on something other than whitespace by calling one of it's functions.
See this for more reference:
http://www.cplusplus.com/reference/iostream/ifstream/[^]
Just noticed you wanted this function prototype:
std::string readWord(ifstream& in)
{
string s = "";
if(in.good())
{
s << in;
}
return s;
}
|
|
|
|
|
hai,
Any one please tell how to get the position in integer values of selected item in ClistControl
Thanks In Advance
Jannath
|
|
|
|
|
int index=m_List.GetNextItem(-1,LVNI_SELECTED);
|
|
|
|
|
|
|
Since you mention values and not value, I am presuming that you are looking for multiple selected items, so I would recommend using CListCtrl::GetFirstSelectedItemPosition(...) and CListCtrl::GetNextSelectedItem(...) .
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
i mean a single value.i am selecting a single value & i want the exact position of that item in the list
Thanks in Advance
|
|
|
|
|
Whitesky's approach is more direct and will work well, but note that my above will also work for single selected items (or for nothing selected as well).
Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|