26

Re: Operation with flows (threads) in MFC

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

27

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> AfxBeginThread Normal end of a flow - simply to quit function, that is return 0;

28

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> Hello, RussianFellow, you wrote: RF>> AfxBeginThread ES> Normal end of a flow - simply to quit function, that is ES> ES> return 0; ES> it is clear. And how to be with function CMyClass:: Calculation, to quit it on demand from the outside? To send the message, and in this function to arrange checks on the message (function invocation GetMessage) with check of value of this message or somehow differently? And how to be in case I launch a flow not by means of AfxBeginthread, and by means of CreateThread or _beginthreadex?

29

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> it is clear. And how to be with function CMyClass:: Calculation, to quit it on demand from the outside? RF> to Send the message, and in this function to arrange checks on the message (function invocation GetMessage) with check of value of this message or somehow differently? Will create two events one to mean a signal to an output, the second - that it is necessary to consider to eat Here something an example just a worker thread which reacts to 2 events - it is necessary to consider something and it is necessary to be completed. Truth implementation of waiting of the main flow it is made crookedly. He stupidly falls asleep in expectation of calculation end. It turns out any asynchrony. It was necessary to send any asynchronous window message from a flow to the main application. RF> and how to be in case I launch a flow not by means of AfxBeginthread, and by means of CreateThread or _beginthreadex? The Same - return 0.

30

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> it is clear. And how to be with function CMyClass:: Calculation, to quit it on demand from the outside? RF> to Send the message, and in this function to arrange checks on the message (function invocation GetMessage) with check of value of this message or somehow differently? RF> And how to be in case I launch a flow not by means of AfxBeginthread, and by means of CreateThread or _beginthreadex? All of them as a result cause CreateThread that I and to you wished. Afx... Something for COM in the beginning does, it is not necessary for you. For end  ahead of time submit in its parameters - there, in structure LPVOID a certain flag,  a variable marked as volatile. And during calculations check this flag on the validity if true - interrupt calculations and rise upwards to most return 0.

31

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> for a suspension of performance of a flow it is necessary to use function SuspendThread, for continuation of performance of a flow - function ResumeThread. It is possible, but it is not necessary. It is better to send to a flow controlling commands - e.g. by "sending" (setting) . Events (NOT windows messages, and those that CreateEvent), or . The variables which value the flow will periodically check. RF> and how competently to destroy a flow in MFC? It is correct to complete flow operation - also as well as not on MFC, as well as in other systems/languages - to tell about it to a flow as - see above. RF> to me advised function TerminateThread Who advised? Result deliver here (c) it is not necessary to use TerminateThread. RF> 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. At it a heap of lacks, be not too lazy, is mandatory read TerminateThread And the flow stack perhaps the only thing that is released (there was truth a bug, read under the link: Windows Server 2003 and Windows XP: The target thread's initial stack is not freed, causing a resource leak.) Here to you : TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems: If the target thread owns a critical section, the critical section will not be released. If the target thread is allocating memory from the heap, the heap lock will not be released. If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent. If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL. + will not be caused  local objects. + if you use MFC AfxBeginThread - any additional pieces, as well as itself CWinThread which creates MFC, also will not be released RF> 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? See above, at the very beginning of RF> There are functions AfxEndThread and ExitThread, caused in a stream function for its end. But how correctly to use them? It is not necessary for you to use them, simply do reset (return) of a stream function, and you will use see will not be caused  local objects.

32

Re: Operation with flows (threads) in MFC

Hello, Glory, you wrote: RF>> And how to be in case I launch a flow not by means of AfxBeginthread, and by means of CreateThread or _beginthreadex? All of them as a result cause CreateThread that I and to you wished. Afx... Something for COM in the beginning does, it is not necessary for you. For end  ahead of time submit in its parameters - there, in structure LPVOID a certain flag,  a variable marked as volatile. And during calculations check this flag on the validity if true - interrupt calculations and rise upwards to most return 0. That is the code should have about such appearance: typedef struct {//structure fields; bool terminate;} MyData; DWORD WINAPI MyThreadFunc (LPVOID lpParam) {MyData* pMyData = (*MyData) lpParam;//function actions; if (pMyData-> terminate == true) {//clearing of resources; return 0;}//function actions; if (pMyData-> terminate == true) {//clearing of resources; return 0;} //function actions; return 0;//end of operation of function MyThreadFunc}?

33

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> That is the code should have about such appearance: No. In such cycle the flow will eat permanently CPU time. It is necessary to use events and to expect them in a flow through WaitForMultipleObjects Purely for an example: HANDLE hEvents [2]; DWORD WINAPI ThreadProc (LPVOID lpParameter) {DWORD dwEvent = 0; bool bExit = false; while (! bExit) {dwEvent = WaitForMultipleObjects (2, hEvents, FALSE, INFINITE); switch (dwEvent) {case WAIT_OBJECT_0://event on an output bExit = true; break; case WAIT_OBJECT_0 + 1://it is necessary to consider event//here there are any calculations break; default://something went not so, we quit bExit = true;}} return dwEvent;} int main () {hEvents [0] = CreateEvent (NULL, FALSE, FALSE, NULL);//event is expected in a flow when the flow should be completed hEvents [1] = CreateEvent (NULL, FALSE, FALSE, NULL);//event is expected in a flow when it is necessary to wake up and something to consider DWORD tid = 0; CreateThread (NULL, 0, ThreadProc, NULL, 0, &tid);//we create worker thread SetEvent (hEvents [1]);//it is necessary to consider return 0;} In MFC there are any classes of a wrapper over all this multithreading, but I feeblly understood with MFC implementation. It is necessary to read books on this library. Generally, in my opinion a multithreading it not that subject which can be mastered for 5 minutes at a forum. It is necessary to read the appropriate literature.

34

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> That is the code should have about such appearance: RF> RF> typedef struct RF> {RF>//structure fields; RF> bool terminate; RF>} MyData;... RF> RF>? Yes, so. I only would make clearing of resources in one place  that is how it is accepted to do it in a C ++ - through visibility areas,  or as there still. Would create one more function DoWork in which all operation would be executed, and its call from MyThreadFunc would envelop in try... catch that for example division into a zero did not take off and did not kill all process. At last, terminate I would make as volatile bool terminate that it was always read from storage - where lies, instead of from any cache. But here a point at issue which I do not understand, here arguing: Here state that volatile in With ++ works as it is necessary http://stackoverflow.com/questions/6995 … ered-wrong And here that in volatile is not  both can not work http://stackoverflow.com/questions/2963 … atile-bool And suggest to use instead of it std:: : atomic <bool> terminate (false) I such do not understand, if this keyword of language what for it to break and if it broke why then not to produce a compilation error at usage of the broken? In general,  such .

35

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> Hello, RussianFellow, you wrote: RF>> That is the code should have about such appearance: ES> Is not present. In such cycle the flow will eat permanently CPU time. You entirely read arguing? Where you see any infinite loop in the resulted code? This  forms for  start of calculations, it for this purpose and is intended to form,  CPU time and to die. And check of a flag of a canceling in the same way as at RussianFellow, it is made in C# CancellationToken. That you offer, this creation constant  which will wait permanently for requests about any calculations. The sense in it is, but then it is already possible to use generally  a pool , and it is even more difficult for . ES> Generally, in my opinion a multithreading it not that subject which can be mastered for 5 minutes at a forum. It is necessary to read the appropriate literature. Well why, if pair-triple days to experiment, it is quite possible to learn. At least, there will be an understanding of about what in the literature write.

36

Re: Operation with flows (threads) in MFC

Hello, Glory, you wrote: you entirely read arguing? Where you see any infinite loop in the resulted code? This  forms for  start of calculations, it for this purpose and is intended to form,  CPU time and to die. And check of a flag of a canceling in the same way as at RussianFellow, it is made in C# CancellationToken. Litter, not attentively read.

37

Re: Operation with flows (threads) in MFC

Hello, Glory, you wrote: Hello, RussianFellow, you wrote: RF>> That is the code should have about such appearance: RF>> RF>> typedef struct RF>> {RF>>//structure fields; RF>> bool terminate; RF>>} MyData;... RF>> RF>>? Yes, so. I see. I had such question - and how correctly to pause a flow and how correctly to launch on continuation the paused flow? If it is undesirable to use functions SuspendThread and ResumeThread; Here the code: typedef struct {//structure fields; int workflag;//a flag: 0 - the flow is fulfilled, 1 - to complete a flow, 2 - to pause a flow, 3 - to launch on continuation the paused flow} MyData; DWORD WINAPI MyThreadFunc (LPVOID lpParam) {MyData *pMyData = (MyData *) lpParam;//function actions; if (pMyData-> workflag == 1)//flow end {//clearing of resources; return 0;} if (pMyData-> workflag == 2)//a flow suspension {//clearing of resources if it is necessary; while ((pMyData-> workflag! =1) && (pMyData-> workflag! =3));//we wait, there will be no yet a command to complete a flow or to continue flow performance if (pMyData-> workflag == 1)//flow end {//clearing of resources; return 0;} if (pMyData-> workflag == 3)//continuation of execution of a flow {//to connect resources if it is necessary; workflag = 0;}}//function actions; if (pMyData-> workflag == 2)//a flow suspension {//clearing of resources if it is necessary; while ((pMyData-> workflag! =1) && (pMyData-> workflag! =3));//we wait, there will be no yet a command to complete a flow or to continue flow performance if (pMyData-> workflag == 1)//flow end {//clearing of resources; return 0;} if (pMyData-> workflag == 3)//continuation of execution of a flow {//to connect resources if it is necessary; workflag = 0;} }//function actions; return 0;//end of operation of function MyThreadFunc} whether is correct at me is written? (Function in a flow sequentially fulfills actions, and is then completed. No infinite loops in it are present).

38

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> I had such question - and how correctly to pause a flow and how correctly to launch on continuation the paused flow? RF> if it is undesirable to use functions SuspendThread and ResumeThread; RF> Here the code: RF> RF> typedef struct RF> {RF>//structure fields; RF> int workflag;//a flag: 0 - the flow is fulfilled, 1 - to complete a flow, 2 - to pause a flow, 3 - to launch on continuation the paused flow RF>} MyData; RF> I wrote to you that the flag should be marked or as volatile, or std:: atomic. As at you it is written - can work through time. Whether RF> it is correct at me it is written? (Function in a flow sequentially fulfills actions, and is then completed. No infinite loops in it are present). My recommendation about one  a flag makes sense only for one simple task - a canceling of performance of a flow. For this purpose that you want to make, with a suspension and renewal - it is better to use recommendations of Skvortsova, look that he wrote to you. But generally, you think - that such "to pause a flow"? And if he from a network reads right now? Or does something else - captured resources which long it is impossible to hold, and here you paused it, resources keep, and it is undesirable so to do. I do not know precisely as the manager of storage present With ++ (them generally works much), but I think that in a multi-threaded surrounding it can quite capture any lock by a call new and if SuspendThread stops performance at this particular time after that all remaining calls new will hang up. Normally for a stop any safe points places in which it is possible to stop with a warranty of become that breaks nothing. SuspendThread such warranties does not give, to you it correctly advised against. Personally I see for the first time such task to "pause" calculation. For what it generally is necessary? What for? I did similar, but not for computing tasks, and for something similar to document circulation. What for to you to pause calculations?

39

Re: Operation with flows (threads) in MFC

Hello, Glory, you wrote: But generally, you think - that such "to pause a flow"? And if he from a network reads right now? Or does something else - captured resources which long it is impossible to hold, and here you paused it, resources keep, and it is undesirable so to do. I do not know precisely as the manager of storage present With ++ (them generally works much), but I think that in a multi-threaded surrounding it can quite capture any lock by a call new and if SuspendThread stops performance at this particular time after that all remaining calls new will hang up. Personally I see for the first time such task to "pause" calculation. For what it generally is necessary? What for? I did similar, but not for computing tasks, and for something similar to document circulation. What for to you to pause calculations? Well any calculations, for example, become. Results are deduced in a console window (no other resources thus are captured). This console window is on a screen background - and in the foreground there is an application dialog box. And if the user wants to look at process of calculations at pushing the dialog box button the screen foreground the console window in which results of calculations are deduced is located - the user sees that has been calculated to a present situation. And then, at pushing other button of a dialog box, this dialog box again is located on the screen foreground, and process of calculations proceeds.

40

Re: Operation with flows (threads) in MFC

Hello, Glory, you wrote: My recommendation about one  a flag makes sense only for one simple task - a canceling of performance of a flow. For this purpose that you want to make, with a suspension and renewal - it is better to use recommendations of Skvortsova, look that he wrote to you. Evgenie wrote to me about other case - when there is a cycle (generally speaking, infinite) in which there is an analysis of events. It does not approach me. I have a serial algorithm which is completed in the absence of influence from the outside and on it the flow is destroyed. But from the outside also it is possible forcedly, ahead of time to complete a flow. Also from the outside it is possible to pause forcedly a flow, and then it either forcedly to continue, or forcedly to complete ahead of time. It seems to me that such construction is admissible: if (pMyData-> workflag == 2)//a flow suspension {//clearing of resources if it is necessary; while ((pMyData-> workflag! =1) && (pMyData-> workflag! =3));//we wait, there will be no yet a command to complete a flow or to continue flow performance if (pMyData-> workflag == 1)//flow end {//clearing of resources; return 0;} if (pMyData-> workflag == 3)//continuation of execution of a flow {//to connect resources if it is necessary; workflag = 0;}} As a last resort, it can be changed a little: if (pMyData-> workflag == 2)//a flow suspension {//clearing of resources if it is necessary; while ((pMyData-> workflag! =1) && (pMyData-> workflag! =3))//we wait, there will be no yet a command to complete a flow or to continue flow performance {Sleep (1000);//a time delay of performance of a flow for one second, as Sleep it is possible to deliver any other number} if (pMyData-> workflag == 1)//flow end {//clearing of resources; return 0;} if (pMyData-> workflag == 3)//continuation of execution of a flow {//to connect resources if it is necessary; workflag = 0;}}

41

Re: Operation with flows (threads) in MFC

Hello, AlexGin, you wrote: AG> is the most correct - not to be fastened on CWinThread (it is superfluous for the given task), to cause _beginthreadex or AfxBeginThread. AG> it is important: both in that and in other case "the function producing mathematical calculations" should be global or static That is if I function which produces calculations, mark as static it can be made stream? And whether there are in this case any singularities on operation with such function in comparison with global stream functions?

42

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Evgenie wrote to me about other case - when there is a cycle (generally speaking, infinite) it is incorrect! The cycle not infinite, it is controlled by the main flow which generated this flow of calculations. There it is possible to expect though 20 events depending on exterior conditions. And only the main flow knows as to command "children". In my primitive example the general course of thought is shown. Generally the present industrial code will look time in 3 it more difficult. Check of return codes etc. Generally it is strange to me to write all it. As a matter of fact I print here the author's translation of help MSDN where exactly same is described, and links to code samples with explanations I result in each answer. I more than once wrote that the almost exhaustive information is in MSDN. Personally I and learned, reading MSDN, trying, being mistaken, thinking, from time to time asking questions at forums. . It suggests All that your English is so bad that at all does not allow to read MSDN. And it is sad. I hardly  in an English, but documents read those freely enough. Your attempts look as at the student burns a lab, there is no time to learn,  all Internet the questions. It I to that you ask the questions simultaneously on several different sites. I once again will repeat - a multithreading it not the most simple subject, in it it is necessary to investigate and read authoritative people. In this connection about a multithreading I cease to answer, for the reason not understanding you of basic principles. Without insults. P.S.  a rating and now you can write to you to a policy. Did not see any message while, despite announcements.

43

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Well any calculations, for example, become. Results are deduced in a console window (no other resources thus are captured). This console window is on a screen background - and in the foreground there is an application dialog box. And if the user wants to look at process of calculations at pushing the dialog box button the screen foreground the console window in which results of calculations are deduced is located - the user sees that has been calculated to a present situation. And then, at pushing other button of a dialog box, this dialog box again is located on the screen foreground, and process of calculations proceeds. It is a classical example of incorrect designing of logic of a software. If there are two windows, the user can stick itself with the mouse and cause a window on  the plan. Concerning the forced emerging recently there was a subject in WINAPI, there all classified. In general, it is necessary to learn .

44

Re: Operation with flows (threads) in MFC

Hello, Evgeniy Skvortsov, you wrote: ES> I more than once wrote that the almost exhaustive information is in MSDN. Personally I and learned, reading MSDN, trying, being mistaken, thinking, from time to time asking questions at forums... ES> it suggests All that your English is so bad that at all does not allow to read MSDN. And it is sad. I hardly  in an English, but documents read those freely enough. My English quite good. Simply MSDN is one, and the explanation of that is written in MSDN is another. And code samples not always on MSDN are. ES> your attempts look as at the student burns a lab, there is no time to learn,  all Internet the questions. It I to that you ask the questions simultaneously on several different sites. Well, correctly, time at me has not enough. ES> I once again will repeat - a multithreading it not the most simple subject, in it it is necessary to investigate and read authoritative people. ES> In this connection about a multithreading I cease to answer, for the reason not understanding you of basic principles. ES> without insults. ES> P.S.  a rating and now you can write to you to a policy. Did not see any message while, despite announcements. That, thanks. I am afraid that me  in section of "Politician". Or for violation of rules of a forum (for example, "kindling of international hatred"), or is simple that moderators will not like that I write.

45

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Hello, Evgeniy Skvortsov, you wrote: RF> My English quite good. Simply MSDN is one, and the explanation of that is written in MSDN is another. And code samples not always on MSDN are. To you already told - esteem Richter before to climb to flows and to write  a software. http://wm-help.net/books-online/book/59464.html 0 - there chapters 6-11. Hundred times would read, than to ask at a forum.

46

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> That is if I function which produces calculations, mark as static it can be made stream? Yes RF> And whether there are in this case any singularities on operation with such function in comparison with global stream functions? Function a member of a class with the modifier static than does not differ from global, besides that is in class name space. All ordinary functions members of a class are caused with the latent pointer on a copy of a class That is if there is a class struct A {int m_a; void f () {m_a ++;} static DWORD WINAPI MyThreadFunc (LPVOID lpParam) {return 0;} }; That at function invocation f (), actually a call goes as f (this) and consequently in function it is possible to address to variables, in particular to m_a. And here inside MyThreadFunc - to address to m_a already it is impossible. As static functions are caused how they are declared, this is not transferred. But to it it is transferred lpParam, therefore at flow creation it is possible to transfer this explicitly AfxBeginThread (MyThreadFunc, this) And in a stream function to use the pointer on a copy of class DWORD WINAPI MyThreadFunc (LPVOID lpParam) {A *a = static_cast <A *> (lpParam); and-> we cause everything return 0;} It is possible to make from MyThreadFunc function an adapter which will call through the pointer normal function a term in which it is possible to work easy a dataful class struct A {int m_a; void f () {m_a ++;} static DWORD WINAPI MyThreadFunc (LPVOID lpParam); DWORD RealThreadFunc () {};} ; DWORD WINAPI A:: MyThreadFunc (LPVOID lpParam) {A *a = static_cast <A *> (lpParam); return and-> RealThreadFunc ();} Pay attention, static functions should be defined with class name instructions. Briefly so.

47

Re: Operation with flows (threads) in MFC

Hello, RussianFellow, you wrote: RF> Hello, AlexGin, you wrote: AG>> is the most correct - not to be fastened on CWinThread (it is superfluous for the given task), to cause _beginthreadex or AfxBeginThread. AG>> it is important: both in that and in other case "the function producing mathematical calculations" should be global or static RF> That is if I function which produces calculations, mark as static it can be made stream? And whether there are in this case any singularities on operation with such function in comparison with global stream functions? To mark as "static" - probably causes a compilation error since there should not be a seamless access to  a class inside. Global functions, also as well as static - are not MEMBERS of the CLASS. All singularities are defined by the given moment. P.S. Here hardly above, companion Evgenie Skvortsov - all is very sensible explained! P.P.S. Jeffrey Richter's book:" Creation  Win-32 applications... "Is that to you, RussianFellow, it is necessary!

48

Re: Operation with flows (threads) in MFC

Hello, AlexGin, you wrote: AG> Global functions, also as well as static - are not MEMBERS of the CLASS. Do not import   to the HARDWARE brain, certainly static methods of a class are its members, them and name: static member_function, but not connected to copies (objects) of a class.

49

Re: Operation with flows (threads) in MFC

Hello, pilgrim _, you wrote: _> do not import a house to the HARDWARE brain _> a house _> a house the Pertinent citation about  from "Down of a house" https://www.youtube.com/watch?v=j2hZgIIaIyI Let's advise  use IOCP. Through it too it is possible  to scatter on flows.

50

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? std:: thread it is able.