把Proactor与Reactor事件集成的演示代码

//file: main.cpp
//author: StoneJiang <jiangtao> http://www.tao-studio.net</jiangtao>
//date: 2008-12-14
//desc: Integrating Proactor and Reactor Events on Windows


#include "ace/Proactor.h"
#include "ace/WIN32_Proactor.h"
#include "ace/Atomic_Op.h"
#include "ace/OS_NS_unistd.h"
 
#include "ace/Reactor.h"
#include "ace/Time_Value.h"

class Timeout_Handler : public ACE_Handler, public ACE_Event_Handler
{
    // = TITLE
    //     Generic timeout handler.

public:
    Timeout_Handler (void)
    {
    }

    // This is called by the Proactor. This is declared in ACE_Handler.
    virtual void handle_time_out (const ACE_Time_Value &tv,
        const void *arg)
    {
        // Print out when timeouts occur.
        ACE_DEBUG ((LM_DEBUG, "(%t|%P) %d timeout occurred for %s @ %d.\n",
            ++count_,
            (char *) arg,
            tv.sec ()));

        // Since there is only one thread that can do the timeouts in
        // Reactor, lets keep the handle_timeout short for that
        // thread.
        if (ACE_OS::strcmp ((char *) arg, "Proactor") == 0)
            // Sleep for a while
            ACE_OS::sleep (1);
    }

    // This method is declared in ACE_Event_Handler.
    virtual int handle_timeout (const ACE_Time_Value &tv,
        const void *arg)
    {
        this->handle_time_out (tv, arg);
        return 0;
    }

private:
    ACE_Atomic_Op <ace_thread_mutex style="color: #0000ff">int&gt; count_;
};


<span style="color: #0000ff">int</span>
ACE_TMAIN (<span style="color: #0000ff">int</span>, ACE_TCHAR *[])
{
    ACE_DEBUG ((LM_DEBUG, "<span style="color: #8b0000">(%t|%P) work starup\n</span>"));
    ACE_Proactor::close_singleton (); 

    ACE_WIN32_Proactor *impl = <span style="color: #0000ff">new</span> ACE_WIN32_Proactor (0, 1); 
    ACE_Proactor::instance (<span style="color: #0000ff">new</span> ACE_Proactor (impl, 1), 1);

     ACE_Reactor::instance ()-&gt;register_handler(impl, impl-&gt;get_handle ());

    Timeout_Handler handler;
    <span style="color: #008000">// Register a 2 second timer.</span>
    ACE_Time_Value foo_tv (2);
    <span style="color: #0000ff">if</span> (ACE_Proactor::instance()-&gt;schedule_timer (handler,
        (<span style="color: #0000ff">void</span> *) "<span style="color: #8b0000">Proactor</span>",
        ACE_Time_Value::zero,
        foo_tv) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "<span style="color: #8b0000">%p\n</span>", "<span style="color: #8b0000">schedule_timer</span>"), -1);

    <span style="color: #008000">// Register a 3 second timer.</span>
    ACE_Time_Value bar_tv (3);
    <span style="color: #0000ff">if</span> (ACE_Reactor::instance ()-&gt;schedule_timer (&amp;handler,
        (<span style="color: #0000ff">void</span> *) "<span style="color: #8b0000">Reactor</span>",
        ACE_Time_Value::zero,
        bar_tv) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "<span style="color: #8b0000">%p\n</span>", "<span style="color: #8b0000">schedule_timer</span>"), -1);


    ACE_Reactor::instance()-&gt;run_event_loop();
    ACE_Reactor::instance ()-&gt;remove_handler (impl,
        ACE_Event_Handler::DONT_CALL);
    ACE_DEBUG ((LM_DEBUG, "<span style="color: #8b0000">(%t|%P) work complete\n</span>"));
    <span style="color: #0000ff">return</span> 0;
}</ace_thread_mutex>
完成源代码下载download

你可能感兴趣的:(thread,windows,.net,OS)