Multithreading with Classes in MFC

Hello,

I am having trouble implementing multi-threading into my program.
At the moment, I am reading the MSDN libraries but it does not make much sense to me.

To start off, I have two different classes that will output to two different files.

For example, a simple example class is given below.

At the moment, I have them outputting one after the other. Although performance isn't really an issue at the moment, I will be eventually be dealing large amounts of data and would like the classes to be doing calculations and output independently on different threads.

I intended to use a Worker Thread but I am unsure how what parameters to pass through to the AfxBeginThread() function.

If anyone could provide any assistance, will be greatly appreciated!

Thank you in advance!
void CLASSA::output()
{
	ofstream output( output1.txt );

	for( unsigned int x = 0; x < 100; x++ )
	{
		output << x << "\n";
	}

	output.close();
}

void CLASSB::output()
{
	ofstream output( output2.txt );

	for( unsigned int x = 0; x < 100; x++ )
	{
		output << x << "\n";
	}

	output.close();
}

Open in new window

LVL 1
mr_stevieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Passing parameters toa worker thread, well you use casting.

eg.
struct X
{
int i;
char s[1024];
...
}


elsewhere
X* x = new X;
fill the values into the object
now cast x as void when passing into the thread and in the thread cast the parameter back to a pointer to an X object.

eg.
AfxBeginThread(MyThreadProc, x);

UINT MyThreadProc(LPVOID pParam)
{
X* x = (X*)pParam;
now use the values passed in
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
>>>> would like the classes to be doing calculations and output independently on different threads.

The calculations is a good tasks for threads to do asynchronously but the output (necessarily) must be done by one thread - normally the main thread. So after calculations the threads should pass their results back to the main thread who make the outputs while the threads exit or do other work:

struct ThreadData
{
    bool finished;
    // put here members for input and result
    ThreadData() : finished(false) { ...}
};

void main_thread()
{
      ThreadData td1;
      fillInputData(1, td1);
      ThreadData td2;
      fillInputData(2, td2);
      beginthread(threadFunc1, 0, &td1);
      beginthread(threadFunc2, 0, &td2);
      // do normal GUI now or wait for the threads to end  if no GUI
      while (!td1.finished  || !td2.finished )
      {
            Sleep(10);
      }
      // coming here both threads were finished and
      // we now can output the results from td1 and and td2

};
0
mr_stevieAuthor Commented:
My apologies for a late reply!

It seems that I wasn't passing the Classes properly when I was passing them through into the ThreadProc. I somehow managed to get my desired output at the moment but I may run into problems down the track.

Thank you for your assistance!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.