Some programming questions

Adrian Tung

Golden Member
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
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
For the thread class, what you do is use the static method and pass in the "this" pointer to it. The static method can then call a specific method of the class.

A very simple, and untested example:

class ThreadClass
{
public:
ThreadClass(){}
void Init()
{
// Use Whatever API to create the thread
// and pass in the 'this' pointer as the parameter
// to the static function ThreadRoute.
}
voud Run();
static void ThreadClass::ThreadRoute(void*);
};

void ThreadClass::ThreadRoute(void* pThreadClass)
{
if(pThreadClass)
{
ThreadClass* pClass = reinterpret_cast<ThreadClass*>(pThreadClass);
pClass->Run();
}
}

void ThreadClass::Run()
{
// Do whatever you want here.. this function's code is running in a separate thread
}