How to support messaging while waiting?

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:

  • Start the message loop from the executing function while waiting.
  • Create a new workflow while waiting and terminate it (correctly) when resuming.

Both options have their drawbacks, to name a few:

For 1:

  • May lead to stack overflow.
  • Perhaps this may be blocked.
  • If the internal message causes the second event to wait for completion, and the external event ends in the meantime, the external function cannot continue until the second event completes, and this situation can expand.

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.

+3
source share
5

++ , , - Windows, MsgWaitForMultipleObjects? - , , - .

+1

EDIT: , " / ".

? (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 - , , ().

, 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(), , . , , .

  • ? , ... ++. . ", " , , .. GC. , .

0

(.. , ..), :

  • ++?

  • ThreadPool ?

    • QueueUserWorkItem?
    • CreateIoCompletionPort?
    • Vista SubmitThreadpoolWork?

, , .

:

( ) (, CreateIoCompletionPort). , (, , , ). , 4. Windows 4 . 4 , , , 4 ( ). , , - (.. -), .

, . - . Thread. .

:

, / , . concurrency. .

, , , , .

0

, ++. , , , Foo(), Foo(). Bar(), .

- , . Continuations - , -, , , . , .

. , . , M1..Mx, F1... Fy, , , . Fi, Mj. , . F1... Fn ; , Fi . , . . .

Mj , . ? , , . , . - , N- , , N-1.

, , . .

0

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

0
source

Source: https://habr.com/ru/post/1710931/


All Articles