|
ringle looks good - the sample code even has th destruciton in it
Now I only need a "Mutex" with zero-cost-initializaiton :-/
(I wanted to avoid the "init Critical Section even if we never need the singleton" implementaiton - see also my reply above)
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
Peter, before you go and adopt the double check solution, please be aware that this idiom, appealing as it looks, is fundamentally broken. The most thorough exposition of the problem is given in Bacon et al. paper The "Double-Checked Locking is Broken" Declaration[^]. This issue has been devoted literally thousands of posts on comp.lang.c++ and comp.lang.c++.moderated, with threading experts rejecting its validity and lots of enthusiastics proposing seemingly correct formulations: one could call this problem the Fermat's Last Theorem of multithreaded programming, if you know what I mean (except that the theorem is true )
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Interesting post Joaquín, thanks. I quickly scanned the referenced article. Out of interest do you have any references to techniques that do work correctly?
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I don't have a copy-and-paste snippet (I wish I did), sorry; seems you have to look for "memory barriers", which ensure the logical ordering of read/writes th DCL pattern relies on. These memory barriers are not portable
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
No problem, I don't actually need code for this (yet). I would have thought that the InterlockedIncrement() et.all. functions would come it to play here.
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
I know, it needs an memory barrier, or a volatile, or... something similar. I was looking for somethng reusable, platform specific, and efficient for larger libraries. I just wanted to check with you guys before following the ideas of my own (which don't work without a dreaded while(cond) Sleep(1)
The solutions I see don't scale well - especially the perfect ones bring in just to much code for a small library, and the acceptable ones are simply not "good enough".
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
In "Modern C++ Design" Alexandrescu says that Double-Checked Locking will work on most processors with most compilers, but you need to check!
Chapter 6 is all about Singletons and worth a read.
I tried to avoid Sleep() except for the real thing.;)
Neville Franks, Author of ED for Windows www.getsoft.com and Surfulater www.surfulater.com "Save what you Surf"
|
|
|
|
|
ringle looks good - the sample code even has destruction in it, which I didn't dare to ask
Now I only need a "Mutex" with zero-cost-initialization :-/
(That was my initial motivation. I want to avoid the "init Critical Section even if we never need the singleton" implementaiton - see also my reply above)
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist Fold With Us! || Agile Programming | doxygen
|
|
|
|
|
Hi!, i hope you guys can help me out.
1.- to get the size of a file (i dont use mfc), i use something like:
HANDLE hfile=CreateFile("myfile.txt", GENERIC_READ, FILE_SHARE_READ, NULL, 0, NULL);
if(hfile)
{
DWORD fsize=GetFileSize(hfile, NULL);
CloseHandle(hfile);
}
is there a better way? like one that doesnt require using W32 API calls? (to keep the code portable)
2.- i now want to read from a file, using ifstream class, but the ifstream::read()function takes an int as the ammount of data to read! what if i want to read say 20MB of data? wont it overflow? . and also (ok ok its 3 questions), is there a better way to read binary (not text) data from a file? i could use the W32 APIs like ReadFile(), ReadFileEx(), etc. but i would like to keep my code as portable and object oriented as possible
thanks!
|
|
|
|
|
1. To get the size of a file, take a look at the _stat function, and the st_size member of the structure it populates.
2. Will an integer overflow? Depends on the compiler. The ANSI standard basically says that an integer can be whatever size the compiler manufacturer wants it to be. In early versions of Visual C++, targetting Windows 3, it was 16 bits. In a specialist compiler for an embedded system, it might only be 8 bits. In Visual C++ 6, its 32 bits. So provided you are using a modern version of Visual C++, you shouldn't have a problem.
3. Not a huge expert on non-Windows C++ development, but I would imagine that anything that uses the STL would be portable.
|
|
|
|
|
|
another way to get the file size (and it's portable too) is to use the ftell function:
FILE *f = fopen("foo.dat","rb");
long lFileSize = 0;
if (f != NULL)
{
fseek(f,0,SEEK_END);
lFileSize = ftell(f);
fclose(f);
}
|
|
|
|
|
I have setup two PCs for multilink connections using two modems each. One PC as dialer and another for receiving call. After the PCs got connected I used "RasEnumConnections" function to get the details of the connections. But I always get only one "RASCONN" structure. If only modem 1 is connected I get the value 1 for the subentry. If only modem 2 got connected I get the value 2 for the subentry. But if both modem got connected I only got 1 structure returned from "RasEnumConnections" and subentry=1. Why don't I get two RASCONN structure? How do I get all the RASCONN for all subentries ?
|
|
|
|
|
Hi !
My application is using an Access database through ODBC.
Now, if I want to make it run on a computer which doesn't have MS Office installed, it doesn't work because there is no ODBC Access driver.
Do you know if it is possible (and how) to install only an ODBC driver for Access and if this is freely installable ?
Thanks for your help !
Jerome
|
|
|
|
|
Hi also
You should install MDAC 2.7 and your problem is resolved. You can get it from MS for free. See ya Kurt
Fast Prototyping
|
|
|
|
|
Additional to what Kurt has said, you do not need to install MDAC on Windows XP or Windows 2003 as they come pre-installed with the OS.
IIRC Windows XP has at least 2.7 and Windows 2003 has at least 2.8
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
I've being trying to get CString directly to float, but maybe I need to go through another class like 'const char *' first.
This is my problem code:
void CTest2Dlg::OnSelchangeList1()
{
int Stock; //Stock item quantity
int ListIndex; //Number of Items in list
int StrLen; //Length if string in list
double TAG; //Stock item cost inc. tax
float RRP; //Stock item rrp inc tax
float GP; //gross profit
CString WorkingStr; //complet list item string
CString TempStr; //extract of WorkingStr
CListBox * List1=(CListBox *)GetDlgItem(IDC_LIST1);
ListIndex = List1->GetCurSel(); //get selected item 0 based
List1->GetText(ListIndex,WorkingStr); //get WorkingStr from list
TempStr = WorkingStr.Mid(47,3); //Extract stock from WorkingStr
SetDlgItemText(IDC_STATIC4,TempStr); //Display stock on dialog
TempStr = WorkingStr.Mid(54,8); //Extract TAG Ex tax from WorkingStr
// problem code
sscanf(TempStr, "%f", TAG); //supposed to assign float TAG value of //CString TempStr. Not working
// end problem code
TAG = TAG * 1.1; //add 10% tax to TAG
TempStr.Format(_T("$%f"),TAG); //Assign TAG to CString
SetDlgItemText(IDC_STATIC5,TempStr);//Display TAG on Dialog
TempStr = WorkingStr.Mid(62,8); //Extract RRP ex tax from workingstr
SetDlgItemText(IDC_STATIC6,TempStr);//display RRP ex tax on dialog
//finish code when sscanf or other like function works
}
Thats the code. Can anyone show me how to get the value of TempStr into TAG? It shouldn't matter, but I'm using MS Embedded Visual C++ writing for Pocket PC
Thanks for any help you can give me!
Jochum Wittebrood
|
|
|
|
|
if you wonna convert from CString to float you can use following
CString sFloatString=_T("5.45");
TCHAR *tmpChar;
float fAValue = _tcstod( sFloatString.GetBuffer(0), &tmpChar );
That should pretty much do the trick
Greetz,
Davy
|
|
|
|
|
CString has an operator that returns a LPCTSTR so calling GetBuffer is not necessary.
__________________________________________
a two cent stamp short of going postal.
|
|
|
|
|
how to create a lpr port in windows 2000 without user interface
ashees
|
|
|
|
|
Hello,
I am testing Rob Grove's SQLite wrapper called CppSQLite posted here at CodeProject, but I am running it in a Dialog based application. (CppSQLite is a great wrapper for SQLite, by the way).
I have some code that is adding lines to a ListBox instead of 'cout'ing it, but it is behaving strangely. At least I think it is! The following is a section of my code:
<snip>
try
{
int i, fld;
time_t tmStart, tmEnd;
CppSQLiteDB db;
SQLResultsStr.Format(_T("SQLite Version: %s"),db.SQLiteVersion());
m_List_Sqlite_Results.AddString(SQLResultsStr);
remove(gszFile);
db.open(gszFile);
SQLResultsStr = _T("Creating emp table...");
m_List_Sqlite_Results.AddString(SQLResultsStr);
db.execDML("create table emp(empno int, empname char(20));");
SQLResultsStr = _T("DML tests");
m_List_Sqlite_Results.AddString(SQLResultsStr);
<snip>
The problem is that no lines are added to the ListBox until the program completes execution. Then they all come flowing in at once!
The question is, why don't the m_List_Sqlite_Results.AddString(SQLResultsStr) statements add the string to the ListBox immediately after they are executed?
I hope this is clear!
Thanks,
Murrah Boswell
|
|
|
|
|
CListBox uses messages to add strings to the list. It may be that the paint messages are not being processed if your application is busy doing something like adding more entries within a tight loop.
You could force the update of the list control after each add with RedrawWindow() , that may have the desired effect.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
Hello,
Thanks for the reply!
The statements are all executed sequentially. I.e., no loops.
Could it be that I turned off precompiled headers for one of my modules that is pure C++ code? This is the module that has all the SQLite database manipulation functions. I could not get the module to compile with precompiled headers, so I turned it off.
The RedrawWindow() command did work, but it does seem like additional overhead.
Thanks,
Murrah Boswell
|
|
|
|
|
otrcomm wrote:
Could it be that I turned off precompiled headers
No, If you have sections of code that rarely change then it is advisable to turn on precompiled headers. Basically they speed up compilation as the compiler does not need to compile all code each time you compile. (You do need to compile it all at least once however!) It is not necessary to have precompiled headers turned on.
otrcomm wrote:
The RedrawWindow() command did work, but it does seem like additional overhead.
Basically what you are doing here is ensuring that the list is redrawn before the code continues to execute. If you do not do this then paint messages are normally processed when the code comes to a natural break.
You may also be able to ease the problem by running the SQL code in another thread (leaving the main thread to process the paint messages) the choice is yours.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain)
|
|
|
|
|
Hello,
Thanks again!
Since I am just testing the CppSQLite code too understand it better, I don't mind the overhead of the RedrawWindow() command now. However, under production code, I will take your advice and create a separate thread for the SQL code.
Another question. When you reply to my messages, I see parts of my message included. What setting at CodeProject do you have turned on to include the message you are replying too? I just get a blank textbox to reply in.
Thanks,
Murrah Boswell
|
|
|
|
|