peakzhang 发表于 2007-12-11 22:03:14

ACE包自带例程的运行问题

最近在学习ACE的线程管理部分,ACE-5.5\ACE_wrappers\examples\APG\ThreadManagement里Signals_vc8.vcproj程序的运行结果怎么会是这样啊?
(1936) Starting thread
(3396) Starting thread
(2664) Starting thread
(2108) Starting thread
(3012) Starting thread
好象信号没有发到。可是程序里明明发啊。

peakzhang 发表于 2007-12-11 22:03:22

程序如下:
// Signals.cpp,v 1.6 2005/12/30 11:02:02 jwillemsen Exp

#include "ace/config-lite.h"
#if defined (ACE_HAS_THREADS)

#include "ace/OS_NS_time.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Task.h"
#include "ace/Log_Msg.h"
#include "ace/Signal.h"

// Listing 1 code/ch13
class SignalableTask : public ACE_Task<ACE_MT_SYNCH>
{
public:
virtual int handle_signal (int signum,
                           siginfo_t *= 0,
                           ucontext_t * = 0)
{
    if (signum == SIGUSR1)
      {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("(%t) received a %S signal\n"),
                  signum));
      handle_alert ();
      }
    return 0;
}

virtual int svc (void)
{
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Starting thread\n")));

    while (1)
      {
      ACE_Message_Block* mb = 0;
      ACE_Time_Value tv (0, 1000);
      tv += ACE_OS::time (0);
      int result = this->getq (mb, &tv);
      if (result == -1 && errno == EWOULDBLOCK)
          continue;
      else
          process_message (mb);
      }

      ACE_NOTREACHED (return 0);
    }

void handle_alert ();
void process_message (ACE_Message_Block *mb);
};
// Listing 1

void
SignalableTask::process_message (ACE_Message_Block *)
{
}

void
SignalableTask::handle_alert ()
{
}

// Listing 2 code/ch13
int ACE_TMAIN (int, ACE_TCHAR *[])
{
SignalableTask handler;
handler.activate (THR_NEW_LWP | THR_JOINABLE , 5);

ACE_Sig_Handler sh;
sh.register_handler (SIGUSR1, &handler);

ACE_OS::sleep (1);

ACE_Thread_Manager::instance () ->
    kill_grp (handler.grp_id (), SIGUSR1);
handler.wait ();
return 0;
}
// Listing 2
#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Task<ACE_MT_SYNCH>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
pragma instantiate ACE_Task<ACE_MT_SYNCH>;
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

#else
#include "ace/OS_main.h"
#include "ace/OS_NS_stdio.h"

int ACE_TMAIN (int, ACE_TCHAR *[])
{
ACE_OS::puts (ACE_TEXT ("This example requires threads."));
return 0;
}

#endif /* ACE_HAS_THREADS */


[ 本帖最后由 peakzhang 于 2007-12-11 22:18 编辑 ]

peakzhang 发表于 2007-12-11 22:03:33

信号好像是Unix下比较常用,windows下 ACE_Thread_Manager::instance () ->kill_grp (handler.grp_id (), SIGUSR1);这个没有被实现,所以返回值是-1.

楼主还是试试用消息吧,ACE_Message_Block.
页: [1]
查看完整版本: ACE包自带例程的运行问题