I have an application based on a message flow artifact. Whenever there is an action that can be blocked, it is implemented as the "callback on completion / evnet trigger" action, so it does not stop the executable.
Although this technique is suitable for most cases, there are situations where it becomes very inconvenient and overly complicates the code.
What I would like to do is constantly process events while waiting in a transparent way, without breaking the function into before / after waiting.
How can I do it?
I had two options:
Both options have their drawbacks, to name a few:
For 1:
Option 2 may simply end up creating more threads.
Of course, there may be other options that I did not think about.
EDIT: Language is C ++, so functions cannot be deleted and run in a simple (portable?) Way. The platform is Windows (API), although I do not think it is important.
++ , , - Windows, MsgWaitForMultipleObjects? - , , - .
EDIT: , " / ".
? (yield return #), , , , .
yield return
: http://msdn.microsoft.com/en-us/magazine/cc546608.aspx
UPDATE:
, ++
.
, -, / .
. : , :
void send_greeting(const std::string &msg) { std::cout << "Sending the greeting" << std::endl; begin_sending_string_somehow(msg, greeting_sent_okay); } void greeting_sent_okay() { std::cout << "Greeting has been sent successfully." << std::endl; }
:
void send_greeting(const std::string &msg) { std::cout << "Sending the greeting" << std::endl; waiter w; begin_sending_string_somehow(msg, w); w.wait_for_completion(); std::cout << "Greeting has been sent successfully." << std::endl; }
waiter operator(), , wait_for_completion - , , ().
waiter
wait_for_completion
, begin_sending_string_somehow , , .
begin_sending_string_somehow
, , . , , , "" , , , , .
, :
class send_greeting { int state_; std::string msg_; public: send_greeting(const std::string &msg) : state_(0), msg_(msg) {} void operator() { switch (state_++) { case 0: std::cout << "Sending the greeting" << std::endl; begin_sending_string_somehow(msg, *this); break; case 1: std::cout << "Greeting has been sent successfully." << std::endl; break; } } };
(). , , . (, , , ).
()
, operator(), , . , , .
operator()
? , ... ++. . ", " , , .. GC. , .
(.. , ..), :
++?
ThreadPool ?
, , .
( ) (, CreateIoCompletionPort). , (, , , ). , 4. Windows 4 . 4 , , , 4 ( ). , , - (.. -), .
, . - . Thread. .
, / , . concurrency. .
, , , , .
, ++. , , , Foo(), Foo(). Bar(), .
- , . Continuations - , -, , , . , .
. , . , M1..Mx, F1... Fy, , , . Fi, Mj. , . F1... Fn ; , Fi . , . . .
Mj , . ? , , . , . - , N- , , N-1.
, , . .
Is your problem with thread synchronization? If this is your problem, why not use a mutex? It can be wrapped with an interface. In fact, you can use the PIMPL idioms to carry the mutex.
http://msdn.microsoft.com/en-us/library/system.threading.mutex(VS.71).aspx
Source: https://habr.com/ru/post/1710931/More articles:Why is an NSWindow or NSView instance handling its own key events and not its delegate? - objective-cIs there an effect on query speed when using SQL_CALC_FOUND_ROWS in MySQL? - mysqlJ2ME лишний раздражающий запрос на доступ к HTTP - httpHow do you feel about percentages and pixels in one CSS element? - cssIPhone Phonebook - Auto Select Phone Owner - iphoneIs it possible to use database link between oracle database and postgresql database on different physical servers - javaShould I study the framework for starting a project? - frameworksSort from current point to end of file in gvim? - vimC # Request Queue - c #поддерживать состояние объекта на разных страницах с помощью PHP - oopAll Articles