Click here to Skip to main content
15,888,802 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Game dev tutorials for c++ Pin
Afzaal Ahmad Zeeshan26-Nov-16 8:45
professionalAfzaal Ahmad Zeeshan26-Nov-16 8:45 
AnswerRe: Game dev tutorials for c++ Pin
CPallini28-Nov-16 21:56
mveCPallini28-Nov-16 21:56 
QuestionUsing multiple cores within a single thread in C++ and MFC Pin
TQTL22-Nov-16 6:43
professionalTQTL22-Nov-16 6:43 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
Richard MacCutchan22-Nov-16 8:38
mveRichard MacCutchan22-Nov-16 8:38 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
Patrice T22-Nov-16 10:54
mvePatrice T22-Nov-16 10:54 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
Randor 22-Nov-16 12:27
professional Randor 22-Nov-16 12:27 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
David Crow22-Nov-16 12:48
David Crow22-Nov-16 12:48 
AnswerRe: Using multiple cores within a single thread in C++ and MFC Pin
leon de boer22-Nov-16 19:15
leon de boer22-Nov-16 19:15 
Just ask OpenMP how many processors it is using
int omp_get_num_procs();

If you want the answer from windows
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
int countCPU = sysinfo.dwNumberOfProcessors;

If you are expecting it to make MFC itself quicker it won't by very much at all unless it's just particular things like graphics or file access that is slowing you down.

To explain why we need to look at MFC which is an event drive system, meaning it fetches messages and dispatches messages that is it's basic behaviour. Part of that behaviour is it needs the events to process in the order they are in the message queue.

For example clicking on the X close button on the top of an application fires off 3 messages
WM_CLOSE
WM_DESTROY
WM_QUIT

Those messages must be processed in that order and the next message can not be started to process until the one before it is completed. So the required ordering of processing of the event queue trumps any multiprocessor threading you want to put on the message queue. Even things like WM_PAINT messages are in order you must paint the windows in that order. It's pretty safe to say most things in the message queue except probably things like WM_MOUSEMOVE are going to have a required order to them and multithreaded or not you can't process the next message without the previous completing. So you can't really speed the MFC message framework up.

The things you can speed up is the single processing of a single message. So in response to a WM_XXXXXXX message you may spawn threads to achieve the required task that is to be executed on that message in a faster time. If what you are doing is safe to windows it is safe to MFC because your threads will all end at exactly the same point as a single threaded program and you will either send back a LRESULT, Send/Post off a message or do some action and MFC would be unaware of the threads.

So basically if you multithread within a single message process or within an action or process that is called you can do it safely from MFC point of view and in fact MFC would be oblivious to the threading.

If you are having problems and crashing you are either not containing yourself to those guidelines and creating order problems for MFC or alternatively what you are doing is just broken on windows and the problem has nothing to do with MFC.

You can split an join threads just using C++11, make a windows console application and paste this code in to see that and mutexing stuff to control. Your OpenMP etc should give better performance but it makes it easy to prototype something.
#include <iostream>
#include <thread>
#include <mutex>

static const int num_threads = 10;

std::mutex mtx;

//This function will be called from a thread

void call_from_thread(int tid) {
	std::cout << "Launched by thread " << tid << std::endl;
}

void call_from_thread_with_mutex (int tid) {
	mtx.lock();
	std::cout << "Launched by thread " << tid << std::endl;
	mtx.unlock();
}


int main() {
    std::thread t[num_threads];

	//Launch a group of threads
	for (int i = 0; i < num_threads; ++i) {
		t[i] = std::thread(call_from_thread, i);
	}
	   
	std::cout << "Non mutex threads launched from the main\n";

	//Join the threads with the main thread
	for (int i = 0; i < num_threads; ++i) {
		t[i].join();
	}


	//Launch a group of threads
	for (int i = 0; i < num_threads; ++i) {
		t[i] = std::thread(call_from_thread_with_mutex, i);
	}

	mtx.lock();
	std::cout << "Mutex threads launched from the main\n";
	mtx.unlock();

	//Join the threads with the main thread
	for (int i = 0; i < num_threads; ++i) {
		t[i].join();
	}

	getchar();

	return 0;
}

In vino veritas


modified 23-Nov-16 2:01am.

QuestionRun time validation in .RC file in VC++ Mfc application Pin
rajmohanpatel21-Nov-16 23:50
rajmohanpatel21-Nov-16 23:50 
AnswerRe: Run time validation in .RC file in VC++ Mfc application Pin
Jochen Arndt22-Nov-16 0:11
professionalJochen Arndt22-Nov-16 0:11 
QuestionTCP server handle Clients, std::map or array? Pin
bestbear20-Nov-16 5:03
bestbear20-Nov-16 5:03 
AnswerRe: TCP server handle Clients, std::map or array? Pin
Afzaal Ahmad Zeeshan20-Nov-16 21:36
professionalAfzaal Ahmad Zeeshan20-Nov-16 21:36 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear20-Nov-16 22:23
bestbear20-Nov-16 22:23 
AnswerRe: TCP server handle Clients, std::map or array? Pin
Jochen Arndt21-Nov-16 0:22
professionalJochen Arndt21-Nov-16 0:22 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear21-Nov-16 1:11
bestbear21-Nov-16 1:11 
GeneralRe: TCP server handle Clients, std::map or array? Pin
Jochen Arndt21-Nov-16 2:35
professionalJochen Arndt21-Nov-16 2:35 
GeneralRe: TCP server handle Clients, std::map or array? Pin
bestbear21-Nov-16 2:48
bestbear21-Nov-16 2:48 
QuestionSimple WaitForSingleObject question Pin
ForNow19-Nov-16 16:14
ForNow19-Nov-16 16:14 
AnswerRe: Simple WaitForSingleObject question Pin
Midi_Mick19-Nov-16 17:09
professionalMidi_Mick19-Nov-16 17:09 
GeneralRe: Simple WaitForSingleObject question Pin
ForNow20-Nov-16 10:48
ForNow20-Nov-16 10:48 
AnswerRe: Simple WaitForSingleObject question Pin
Midi_Mick20-Nov-16 14:17
professionalMidi_Mick20-Nov-16 14:17 
QuestionComponent "Assimp" and Export 3D Scene into Graphical File Pin
Onic77719-Nov-16 11:47
Onic77719-Nov-16 11:47 
AnswerRe: Component "Assimp" and Export 3D Scene into Graphical File Pin
leon de boer19-Nov-16 19:17
leon de boer19-Nov-16 19:17 
Questionpkg_config ?? Pin
Vaclav_18-Nov-16 14:27
Vaclav_18-Nov-16 14:27 
AnswerRe: pkg_config ?? Pin
Richard MacCutchan18-Nov-16 21:55
mveRichard MacCutchan18-Nov-16 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.