|
I don't need any kind of a waitable lock. On a mutex I can't ask 'IsLocked' and get an ansewer right away without waiting. I only want a 'flag' that I can 'set and get' ( True/False ) from any process/thread ( Naming is required for locating it of course ).
This comes any clearer? damn I hope so...
--BlackSmith--
/*The roof is on fire, we don't need no water, let the MF burn*/. BHG.
|
|
|
|
|
>> This comes any clearer?
no i'm afraid not.
>> I don't need any kind of a waitable lock
it sounds like u do from ur description:
>> I only want a 'flag' that I can 'set and get' ( True/False )
also
>> On a mutex I can't ask 'IsLocked' and get an ansewer right away without waiting.
not true
if( WAIT_TIMEOUT == WaitForSingleObject( mymutex, 0))
// it's not signaled
else
// it's signaled
-pete
"No matter where you go, there your are..." - Buckaoo Banzi
-pete
|
|
|
|
|
Indeed, some kernel objects can be queried using WaitForSingleObject (events, semaphores, mutexes ...). Here's a sample that I didn't bother to build or test, but it should be pretty close to what you need:
class CSharableObject
{
public:
CSharableObject( const TCHAR * p_pszName ) :
c_hMutex( NULL )
{
c_hMutex = OpenMutex( SYNCHRONIZE, FALSE, p_pszName );
if( c_hMutex == NULL )
{
c_hMutex = CreateMutex( NULL, FALSE, p_pszName );
if( c_hMutex == NULL )
{
}
}
}
~CSharableObject()
{
if( c_hMutex != NULL )
{
CloseHandle( c_hMutex );
c_hMutex = NULL;
}
}
bool IsLocked()
{
DWORD a_dwReason = WaitForSingleObject( c_hMutex, 0 );
if( a_dwReason == WAIT_TIMEOUT )
return true;
return false;
}
unsigned long Lock( unsigned long p_ulTimeout = INFINITE )
{
return WaitForSingleObject( c_hMutex, p_ulTimeout );
}
bool Unlock()
{
return ReleaseMutex( c_hMutex ) != 0;
}
protected:
HANDLE c_hMutex;
};
Now, whereever you need to use the shared resource:
CSharableObject a_oLocker( _T("SomeUniqueName") );
a_oLocker.Lock();
a_oLocker.Unlock();
if( !a_oLocker.IsLocked() )
{
}
else
{
}
It would be a good idea to make this more robust etc, but it's a start.
Chris Richardson
C/C++ Include Finder[^]
|
|
|
|
|
Is there a way to add a menu to a dialog based application? I could start over again with a Single Document design, but if there's a relatively simple way to add a menu to my existing dialog based app, it would be the least painful for me.
Thanks.
|
|
|
|
|
|
That was easy!
Thanks
|
|
|
|
|
I am working with the VC++ 7 build-in CImage class.
there is a
CImage::SetTransparentColor(int nIndex);
but if i use
m_bmpMyBitmap.SetTransparency(0); /* which should set color 0 in palette transperent */
m_bmpMyBitmap.Save(dlg.GetPathName(), GUID_NULL);
and save it to gif or png the color is not transperent
|
|
|
|
|
What is the most reliable way to convert a float variable to a double variable?
The reason I ask is this:
float fData=2.7;<br />
double dData=fData;
After the above operation dData is: 2.7000000476837158
not 2.7
I know this is probably due to some problem with .7 being represented accurately in binary. How do I ensure that 2.7 float = 2.7 double without resorting to string based conversions and truncating etc?
|
|
|
|
|
there is no easy way to do this, since both float and double are approximations.
i believe the best you can do is to do the float->double cast, then choose an arbitrary precision (say 6 decimal places) and round up or down.
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Hi Chris, yes that would be perfect (2 decimal places actually). Is there a function that will round a float in any of the libraries that you know of? (reason I ask is that I can't seem to find anything applicable)
|
|
|
|
|
i'd start with this:
int scale = 1000;
f*=scale;
if(f>0)
f+=0.5;
else
f-=0.5;
double d =(double)(((double)(int)f) / scale);
-c
Be very, very careful what you put into that head, because you will never, ever get it out. --Thomas Cardinal Wolsey
|
|
|
|
|
Hi Chris, thanks for that info, however it results in:
2.7000000000000002
As did another (less elegant) double rounding function along similar lines I just came across on the net.
Wierd.
|
|
|
|
|
Hmmmm, I tried your example using my version of round and it gave me 2.7000000000000:
double round(double value, unsigned decimalPlaces)
{
double scale = pow(10.0, (double)decimalPlaces);
double temp = value * scale;
temp += (value < 0 ? -0.5 : 0.5);
return (int)temp / scale;
}
By the way, I also got a compiler warning with the float assignment, so I had to add an "f" to the end of the value to get rid of it:
float fData = 2.7f;
Regards,
Alvaro
All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
|
|
|
|
|
Oops! I guess that 2.7000002 *is* the double representation of 2.7.
Thanks for your help, ignore my other reply.
|
|
|
|
|
The problem is that you don't have a 2.7 to begin with.
Instead of trying to represent 2.7 in a double, you are trying to extend a float value which is close to 2.7 to a double. In other words, there is a huge range of double values which come close to a float value of 2.7 while only one is actually a double value of 2.7.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Sure, I know that!
My question was what is a reliable way to convert them.
I guess I should have mentioned that I only ever care about 2 decimal places of precision. (users are entering hours of labour worked and it's always 2 decimal places in the user interface)
|
|
|
|
|
In that case, don't use doubles at all, use scaled integers!!! That's what they do in the commercial world when trying to represent money - find the smallest atomic unit & count in multiples of that....
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Yes it looks like that's what is required for the conversion, unfortunately I can't change the fact that at one end is a float and at the other end is a double because I'm working between two COM components from different developers.
|
|
|
|
|
Well...knowing the representation of floats and doubles (IEEE754), you could use code like this (it'll work for normalized floating point numbers).
float x = whatever;
long y = *(long*)(&x);
const __int64 signBit = (y & 0x80000000) << 32;
const __int64 exponent = (__int64( ((y >> 23)&0xff) - 127 + 1023)) << 52;
const __int64 fraction = (__int64(y & 0x007fffff)) << 29;
__int64 ly = signBit | exponent | fraction;
double lx = *(double*)(&ly);
This will convert float => double with no loss of precision or change in value.
However, I think you'll find that it is the same operations as are done by the floating point part of the processor when it does the float => double conversion, in which case, you might just as well use a straight C assignment...
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
I remember I had this problem before too and I had to do it as the following:
<br />
float flt = 2.7f;<br />
TCHAR sz[65] = _T("");<br />
_stprintf(sz, _T("%.3f"), flt);
<br />
double dbl = _tcstod(sz, NULL);<br />
Now, if you watch the value of dbl you'll see 2.7000000000000, which is what you expected...
|
|
|
|
|
I want to set some member variables of my CDialog (modeless).
SO I do stuff like
CImageDisplay* pImageDisplay = new CImageDisplay;
if (pImageDisplay == NULL) return;
BOOL ret = pImageDisplay->Create(IDD_IMAGEDISPLAY, this);
Where do I put pImageDisplay->width = 100; ? Before the create() or after the create()?
Appreciate your help,
ns
|
|
|
|
|
I tried both - it doesnt seem to matter...
Appreciate your help,
ns
|
|
|
|
|
It doesn't matter because you've reserved memory space for the dialog once you have declared the dialog variable.
When you create the dialog you are telling windows to attach a dialog resource,which is the parent window...
But the class it's already created once you've declared the variable.
hope this helps...
|
|
|
|
|
Thats really interesting - I was trying to deduce the sequence of events with stepping and breakpoints but the IDD_DIALOG resource connection didnt show up with my strategy so your response makes that clearer!! Thanks.
Appreciate your help,
ns
|
|
|
|
|
Fellow cpians , how can I hook to a file to catch that the file changed ?
I'm must code a application that is always monitoring a file on the filesystem and every time that the file is updated , I need to be notified of the fact to reload the file data in my COM Server.
Some Windows message , DDE event , hooking mechanism (In a long distant past I already have done something like this, but it's part of my forgotten memories)???
Thanks in advance.
Cheers,Joao Vaz
And if your dream is to care for your family, to put food on the table, to provide them with an education and a good home, then maybe suffering through an endless, pointless, boring job will seem to have purpose. And you will realize how even a rock can change the world, simply by remaining obstinately stationary.-Shog9
Remember just because a good thing comes to an end, doesn't mean that the next one can't be better.-Chris Meech
|
|
|
|