1

Topic: Operation with flows (threads) in MFC

Whether prompt, dear colleagues how is better to work with flows (threads) in MFC? There is a function producing mathematical calculations. This function big on length and its actions happen in a cycle before performance of a certain condition. This function works in a console mode - input and output of the data there happens to the help of a console window. How it is better to place this function in a flow - to use class CWinThread, to use function AfxBeginThread or to use functions WinAPI for operation with flows?

2

Re: Operation with flows (threads) in MFC

std:: thread

3

Re: Operation with flows (threads) in MFC

Hello, _Butch _, you wrote: _B _> std:: thread And if function which I want to place in a flow, is a member of a class - that how to be?

4

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Hello, _Butch _, you wrote: _B _>> std:: thread RF> And if function which I want to place in a flow, is a member of a class - that how to be? #include <iostream> #include <chrono> #include <thread> class Dumb {private: int id; public: Dumb (int _id): id (_id) {} void stupid () {for (int i = 0; i <10; i ++) {using namespace std:: chrono_literals; std:: cout <<id <<"is thinking..." <<std:: endl; std:: this_thread:: sleep_for (1s);}}}; int main () {Dumb d1 (1); std:: thread t1 (&Dumb::stupid, &d1); Dumb d2 (2); std:: thread t2 (&Dumb::stupid, &d2); t1.join (); t2.join (); return 0;}

5

Re: Operation with flows (threads) in MFC

At me such question - how to work with class CWinThread? How to anchor a certain function to flow CWinThread? (Me interests both an ordinary function, and class function-member). Whether It is necessary to create descendant class from CWinThread that in it a certain function, or enough class CWinThread was fulfilled? If it is necessary to create descendant class from CWinThread what methods there should be redefined? And how to destroy a flow? (For flow creation function CWinThread:: Create, for start - function CWinThread:: SuspendThread, for continuation of operation of a flow - function CWinThread:: ResumeThread is used and how forcedly to destroy a flow, without waiting completions of its performance?)

6

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> at me such question - how to work with class CWinThread? Not  with CWinThread - for worker threads it of that is not necessary. AfxBeginThread, CreateThread, std:: thread - on a choice Briefly why it is not necessary CWinThread - there inside there is a flow and the cycle of messages, as at the main flow of application is twisted. It is necessary to redefine the virtual function Run. But that the flow would be completed, simply to quit this function insufficiently. The flow will live, yet does not receive message WM_QUIT, further it automatically  and bangs all data. If there are any variable members which are necessary after operation end, it is necessary to put before creation a variable bAutoDelete = FALSE and hands to release storage. In general for a simple flow "something to count in a background" too many unnecessary dances with a tambourine.

7

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> Hello, RussianFellow, you wrote: RF>> at me such question - how to work with class CWinThread? ES> Not  with CWinThread - for worker threads it of that is not necessary. ES> AfxBeginThread, CreateThread, std:: thread - on a choice Well and as then at usage AfxBeginThread or CreateThread to pause a flow, then to launch it on continuation how to destroy a flow? You could not result examples (elementary) how to use AfxBeginThread and CreateThread or to give the reference to these examples?

8

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> you could not result examples (elementary) how to use AfxBeginThread and CreateThread or to give the reference to these examples? Richter about flows esteem, before into all it to get.

9

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> it is good, and as then at usage AfxBeginThread or CreateThread to pause a flow, then to launch it on continuation, It is possible to create a flow initially stopped installing a flag dwCreationFlags in CREATE_SUSPENDED to Continue performance - ResumeThread to Pause - SuspendThread RF> how to destroy a flow? Regular end - simply to quit a stream function. Emergency - TerminateThread It is desirable to cause CloseHandle after the termination of operation of a flow Detail information RF> you could not result examples (elementary) how to use AfxBeginThread and CreateThread or to give the reference to these examples? As usual

10

Re: Operation with flows (threads) in MFC

Hello, aloch, you wrote: A> Hello, RussianFellow, you wrote: RF>> you could not result examples (the elementary) how to use AfxBeginThread and CreateThread or to give the reference to these examples? A> Richter about flows esteem, before into all it to get. How the book is called?

11

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> As the book is called? J. Richter. Windows for professionals. Creation of effective WIN32-appendices taking into account specificity of 64-digit Windows version. Publishing houses: Peter, Russian Edition, 2001, 752 p. ISBN 5-272-00384-5, 1-57231-996-8

12

Re: Operation with flows (threads) in MFC

And still a question: how to place in a flow function which accepts not one, and we tell, four parameters?

13

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> And still a question: how to place in a flow function which accepts not one, and we tell, four parameters? Transfer all parameters through LPVOID lpParameter, structure create, push in it all parameters and the address of this structure give stream functions.

14

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: whether RF> prompt, dear colleagues how is better to work with flows (threads) in MFC? _beginthreadex - in MSDN we look; As a last resort - AfxBeginThread (we look in the same place) RF> There is a function producing mathematical calculations. This function big on length and its actions happen in a cycle before performance of a certain condition. This function works in a console mode - input and output of the data there happens to the help of a console window. A condition - we supervise call WinAPI-shnoj:: WaitForSingleObject well or:: WaitForMultipleObjects - here depends on that, there is one condition of loop termination or a little. RF> As it is better to place this function in a flow - to use class CWinThread, to use function AfxBeginThread or to use functions WinAPI for operation with flows? Most correctly - not to be fastened on CWinThread (it is superfluous for the given task), to cause _beginthreadex or AfxBeginThread. IMPORTANT: Both in that and in other case "the function producing mathematical calculations" should be global or static, if this function - simply method of a class the given decision WILL not work!

15

Re: Operation with flows (threads) in MFC

Hello, AlexGin, you wrote: AG> it is important: both in that and in other case "the function producing mathematical calculations" should be global or static, AG> if this function - simply method of a class the given decision WILL not work! I see. And still a question: whether the flow (a variable of type HANDLE can or CWinThread) to be a member of a class?

16

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Hello, AlexGin, you wrote: AG>> it is important: both in that and in other case "the function producing mathematical calculations" should be global or static, AG>> if this function - simply method of a class the given decision WILL not work! RF> it is clear. RF> and still a question: whether the flow (a variable of type HANDLE can or CWinThread) to be a member of a class? Or the pointer on a flow (a variable of type HANDEL* or CWinThread *)?

17

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF>> And still a question: whether the flow (a variable of type HANDLE can or CWinThread) to be a member of a class? RF> or the pointer on a flow (a variable of type HANDEL* or CWinThread *)? It is finite can, what can prevent it?

18

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF>> And still a question: whether the flow (a variable of type HANDLE can or CWinThread) to be a member of a class? RF> or the pointer on a flow (a variable of type HANDEL* or CWinThread *)? Perhaps certainly, the given variable to be a member of a class. I mentioned only that fact that head function of a worker thread - does not happen a member of a class.

19

Re: Operation with flows (threads) in MFC

As I understood, for flow start in MFC it is necessary to use functions AfxBeginThread or CreateThread or _beginthreadex, for a suspension of performance of a flow it is necessary to use function SuspendThread, for continuation of performance of a flow - function ResumeThread. And how competently to destroy a flow in MFC? To me advised function TerminateThread, but it has lacks - after its call the flow all the same occupies a stack of storage and has access to a kernel before the end. And how competently to destroy a flow that at it after that destructions - that after that destructions this flow was not fulfilled, had no access to a kernel and did not occupy a storage stack? There are functions AfxEndThread and ExitThread, caused in a stream function for its end. But how correctly to use them? And how correctly to send messages to a flow and how correctly the flow should accept and process these messages?

20

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> There are functions AfxEndThread and ExitThread, caused in a stream function for its end. But how correctly to use them? RF> and how correctly to send messages to a flow and how correctly the flow should accept and process these messages? Look, as it is made  CancellationToken and make as. During calculations the flow can check a state certain volatile a flag, and if it is cocked to complete calculation, to interrupt a cycle, a recursion, etc. Generally, anything it is not necessary to destroy. The flow itself will be completed, when quits initial function then it  can be checked up something like WaitForSingleObject with zero  and on return code to learn that the flow is completed then to close . The result of operation of a flow can be added in a certain general variable, progress of its operation can be written down too in volatile a variable and periodically to check from the main flow - on the timer, for example.

21

Re: Operation with flows (threads) in MFC

Here at me such situation: there is stream function MyThreadFunc to which necessary parameters in a variable of type LPVOID are transferred. Function MyThreadFunc translates the data from LPVOID in structure from which it transfers the data from this structure in the form of function parameters MyFunc2 (this function is not a member of a class). Function MyFunc2 at first creates object obj class CMyClass, and then causes function obj. Calculate class CMyCLass in which mathematical calculations are produced. How it is better to me to complete in this case performance of a flow to which there corresponds function MyTreadFunc? To cause function TerminateThread from external function (that is therefrom, this flow whence has been caused), to use AfxEndThread or ExitThread in function MyThreadFunc, to send the message from external function to this flow, and then it to process, change the code of function CMyClass:: Calculate that the output from it on obtaining of a certain message or any other sign was possible?

22

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Here at me such situation: there is stream function MyThreadFunc to which necessary parameters in a variable of type LPVOID are transferred. What function launches a flow?

23

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> Hello, RussianFellow, you wrote: RF>> Here at me such situation: there is stream function MyThreadFunc to which necessary parameters in a variable of type LPVOID are transferred. ES> What function launches a flow? OnButton1Click

24

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Hello, Evgeniy Skvortsov, you wrote: ES>> Hello, RussianFellow, you wrote: RF>>> Here at me such situation: there is stream function MyThreadFunc to which necessary parameters in a variable of type LPVOID are transferred. ES>> What function launches a flow? RF> OnButton1Click oh, I apologize, misunderstood. The flow is launched, say, by function AfxBeginThread.

25

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> OnButton1Click Is not present, I meant what function is used for flow creation? AfxBeginThread, CreateThread or _beginthread?