Multithreading with Classes in MFC

Posted on 2010-03-25
Medium Priority
Last Modified: 2013-11-20

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";


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

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


Open in new window

Question by:mr_stevie
LVL 45

Accepted Solution

AndyAinscow earned 1000 total points
ID: 28655673
Passing parameters toa worker thread, well you use casting.

struct X
int i;
char s[1024];

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.

AfxBeginThread(MyThreadProc, x);

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

Assisted Solution

itsmeandnobodyelse earned 1000 total points
ID: 28658329
>>>> 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 )
      // coming here both threads were finished and
      // we now can output the results from td1 and and td2


Author Comment

ID: 29150484
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!

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question