1

Topic: The dynamic list of flows and end

All kind time of days, the colleague Something a little confused I, whether  clever throw? The task is that: a certain service for servicing creates on a flow for everyone a principal flow sleeps and each client flow expects a command on program termination fulfills any actions while the client is not disconnected in this case a flow is normally completed (we quit working function, fulfilling  ..) in case of obtaining by a principal flow of a command on end it signals to client flows and those, accordingly, interrupt operation with the client and correctly are completed a principal flow is obliged to wait normal end of all flows (join) and only then to be completed itself//somewhere in a principal flow std:: list <std:: thread> clients;//the list of client flows Event stopEvent; //a certain object for the event notification auto clientThreadProc ([&stopEvent] ()) {//is worked with the client, the client yet//or yet  stopEvent//will not be disconnected after that we quit function});//actually flows  dynamic on demand from clients//but for simplicity let will be so for an essence does not change clients.emplace_back (clientThreadProc); clients.emplace_back (clientThreadProc);..... WaitForSignalToStop ();//here we sleep and we wait for a command on end stopEvent.set ();//we signal to flows, informing that while to be brief for (auto &thread: clients)//we wait for end of each client flow {thread.join ();} Actually, a problem that I can not invent in any way how correctly to remove a flow from the list clients in a case when he is completed to wait independently and correctly its completions thus. The idea to launch an additional flow for tracing of a state of flows in the list and its cleaning seems to me . Probably logically would look if the completed flow deleted itself from the list, but in this case it is impossible to wait  its completions (join itself in a stream function predictedly leads ) Thankful in advance.

2

Re: The dynamic list of flows and end

Hello, qperz, you wrote: Q> Actually, a problem that I can not invent in any way how correctly to remove a flow from the list clients Q> in a case when he is completed to wait independently and correctly its completions thus. Let remain in the list. On them join too it is possible to make

3

Re: The dynamic list of flows and end

Hello, uzhas, you wrote: U> let remain in the list. On them join too it is possible to make Mmmm though very much a considerable quantity of clients and it is not supposed (to pair hundreds a day) service will work without restarting long time,  storage not the best idea, in my opinion... So it is possible to oblige certainly an additional flow from time to time to fulfill cleaning or generally on idle principal it to lay, but the soul demands something simple and refined)

4

Re: The dynamic list of flows and end

Hello, qperz, you wrote: Q>  storage not the best idea, in my opinion... To select a flow for the user - too not the best idea, well all right I can advise in a principal flow periodically to clean a collection (we can wait a stoplight with  for 5 minutes, then go to clean a collection) as the flow can inform on the output, it is possible to esteem here: https://stackoverflow.com/questions/909 … ll-running

5

Re: The dynamic list of flows and end

Hello, qperz, you wrote: remove queue, get the atomic counter, adding  -  the counter, deleting - decrement and notify about removal. The main  let waits  in a cycle while the counter not the zero

6

Re: The dynamic list of flows and end

A> remove queue, get the atomic counter, adding  -  the counter, deleting - decrement and notify about removal. The main  let waits  in a cycle while the counter not zero By the way, idea interesting, thanks. Though a problem all the same does not solve: nobody guarantees that the client flow has time to be completed after decrements the counter.

7

Re: The dynamic list of flows and end

U> to select a flow for the user - too not the best idea, well all right It agree. Well, more precisely, not always it is the best idea. But for the given task with a separate flow it is possible to consider the decision comprehensible. U> I can advise in a principal flow periodically to clean a collection (we can wait a stoplight with  for 5 minutes, then go to clean a collection) U> as the flow can inform on the output, it is possible to esteem here: https://stackoverflow.com/questions/909 … ll-running Thanks. In general I too came to the same decision. It is better than anything in a head does not come. To notify I be able, in this respect questions are not present)

8

Re: The dynamic list of flows and end

Hello, Kodt, you wrote: It seems that it is possible to replace flows with futures. Unlike a flow which state it is possible to wait only, at the future it is possible to check a state. (wait_for (0s) returns future_status:: ready or deferred/timeout). To delete futures from the list is all the same the task principal (or one more auxiliary) a flow. Worker threads can prompt only before death "me it is time to delete" (to add itself in the list "to immediate "). But if before death the worker thread for some reason did not make it, - here specially for this purpose and it is necessary to interrogate periodically all list on a subject "you still work?" By the way, and after all and truth futures together with async very much even interesting idea of the operation it is even not necessary  removal itself from the list simply principal flow periodically interrogates a state of futures and deletes all with future_status:: ready and at stoplight obtaining it is possible to make remained wait () - the analog join many thanks turns out, I will try to implement!

9

Re: The dynamic list of flows and end

Hello, Kodt, you wrote: It seems that it is possible to replace flows with futures. Similar council can be found here (when you search thread.timed_join ()): https://stackoverflow.com/questions/994 … hread-join a question to experts: why the flow does not have method join with ?

10

Re: The dynamic list of flows and end

Q> Something I was a little confused, whether  clever throw? Please. The counter of flows helps. Unify start and end of flows that always the nobility, how many they are fulfilled at present time. Let the flow kills itself. At end of any flow check an amount available at present and if this amount became equal to zero, clear up the main flow a signal and let it simply quits the program, not . Restriction of this model that if suddenly any flow will be torn down and cannot correctly reduce the counter, the program will never be completed, as this counter never reaches zero. But all is elementary. If suddenly it is necessary to complete flows it is just necessary to expose a flag - normal  a variable. Each flow should check it from time to time. Not mandatory in all cycles, the main thing - that speed of program termination was correct. It within 5 seconds.