- Oct 10, 1999
- 1,370
- 1
- 0
My first question involves C++ classes and threads.
I wonder if it is good design/architecture to do something like this:
A class with a static function meant to run as a thread by calling an initializing function (which is a public member of the class). I've tried, and also been told, that I can't just create a thread using a member function of a class unless I declare it as static. Since a static function is involved, I'll need to also include checks to make sure that only one instance of the class is ever declared and/or called. Basically, everything is encapsulated in the class, including the necessary critical section variables.
So, as an example:
// declare class
CMyThreadClass CApp;
// start running thread
if (!CApp.Init()) // calling this func. will also check if a thread has already been started
{
return -1;
}
// this call should fail
CMyThreadClass CApp2;
if (!CApp2.Init())
{
// yup, it fails
}
// wait for thread to finish or end it
while (CApp.IsRunning())
{
LPARAM lParam;
HPARAM hParam;
int iMsg = CApp.GetMsg(&lParam, &hParam);
switch (iMsg)
{
case WM_CLOSE:
CApp.Stop();
break;
}
}
So is this a good idea? Or is there a better way to do things (so that I can keep a class running a thread happily on its own) without outside intervention (like, say, having to declare a global function outside the class and use that global function to call the class' looping member function)?
And to my second question,
I'm thinking of writing a simple HDD/CD-ROM benchmarking app to help me with some of my hardware tests. I'm trying to get the most accurate real world results as possible, so I thought of a few functions:
i) Test writing speed (single file) - Algorithmically generate a temp file of size X and write it to the drive in blocks of size Y, taking down the time to complete the operation.
ii) Test writing speed (multiple files) - Algorithmically generate a random directory tree structure with X folders and Y files, and write them to the drive in blocks of size Z, taking down the time to complete the operation.
iii) Test read speed - Read random existing files from the drive, timing each of them, and then taking averages.
iv) Test copying speed (single file, to another drive) - Read file generated in (i) and write it to another drive. Take the time needed to complete the operation.
v) Test copying speed (single file, onto same drive) - Read file generated in (i) and write it to the same drive under a different file name.
vi) Test copy speed (multiple files, to another drive)
vii) Test copy speed (multiple files, onto same drive)
For (iv)-(vii), I was wondering if I should just use the SHFileOperation function (which, I assume will best reflect real world performance), or is there any reason for me to write my own functions (using fread/fwrite or CreateFile/ReadFile/WriteFile)?
Anything that I missed out? Or is this a bad idea?
That's all for now, thanks in advance for any comments, ideas and/or criticism.
atwl
I wonder if it is good design/architecture to do something like this:
A class with a static function meant to run as a thread by calling an initializing function (which is a public member of the class). I've tried, and also been told, that I can't just create a thread using a member function of a class unless I declare it as static. Since a static function is involved, I'll need to also include checks to make sure that only one instance of the class is ever declared and/or called. Basically, everything is encapsulated in the class, including the necessary critical section variables.
So, as an example:
// declare class
CMyThreadClass CApp;
// start running thread
if (!CApp.Init()) // calling this func. will also check if a thread has already been started
{
return -1;
}
// this call should fail
CMyThreadClass CApp2;
if (!CApp2.Init())
{
// yup, it fails
}
// wait for thread to finish or end it
while (CApp.IsRunning())
{
LPARAM lParam;
HPARAM hParam;
int iMsg = CApp.GetMsg(&lParam, &hParam);
switch (iMsg)
{
case WM_CLOSE:
CApp.Stop();
break;
}
}
So is this a good idea? Or is there a better way to do things (so that I can keep a class running a thread happily on its own) without outside intervention (like, say, having to declare a global function outside the class and use that global function to call the class' looping member function)?
And to my second question,
I'm thinking of writing a simple HDD/CD-ROM benchmarking app to help me with some of my hardware tests. I'm trying to get the most accurate real world results as possible, so I thought of a few functions:
i) Test writing speed (single file) - Algorithmically generate a temp file of size X and write it to the drive in blocks of size Y, taking down the time to complete the operation.
ii) Test writing speed (multiple files) - Algorithmically generate a random directory tree structure with X folders and Y files, and write them to the drive in blocks of size Z, taking down the time to complete the operation.
iii) Test read speed - Read random existing files from the drive, timing each of them, and then taking averages.
iv) Test copying speed (single file, to another drive) - Read file generated in (i) and write it to another drive. Take the time needed to complete the operation.
v) Test copying speed (single file, onto same drive) - Read file generated in (i) and write it to the same drive under a different file name.
vi) Test copy speed (multiple files, to another drive)
vii) Test copy speed (multiple files, onto same drive)
For (iv)-(vii), I was wondering if I should just use the SHFileOperation function (which, I assume will best reflect real world performance), or is there any reason for me to write my own functions (using fread/fwrite or CreateFile/ReadFile/WriteFile)?
Anything that I missed out? Or is this a bad idea?
That's all for now, thanks in advance for any comments, ideas and/or criticism.