OpenH323中线程创建的方式

在分析线程模型时需要注意OpenH323中线程的实现有多种方式。

1.继承PThread_H323

例子如下:

class OpalH224ReceiverThread : public PThread_H323
{
  PCLASSINFO(OpalH224ReceiverThread, PThread_H323);
    
public:
    
  OpalH224ReceiverThread(OpalH224Handler *h224Handler, RTP_Session & rtpSession);
  ~OpalH224ReceiverThread();
    
  virtual void Main();
    
  void Close();
    
private:
        
  OpalH224Handler *h224Handler;
  RTP_Session & rtpSession;

  PSyncPointAck  exitReceive;
  PBoolean threadClosed;

  unsigned lastTimeStamp;
};

2.将PThread_H323作为成员变量

例子如下:

class H323Transport : public PIndirectChannel
{
  PCLASSINFO(H323Transport, PIndirectChannel);

  public:
    ...

  protected:
    ...
    PThread_H323      * thread;      /// Thread handling the transport
    ...
};
void H323Transport::AttachThread(PThread_H323 * thrd)
{
  PAssert(thread == NULL, PLogicError);
  thread = thrd;
}

3.直接创建线程投入运行

PBoolean H323Transaction::HandlePDU()
{
  int response = OnHandlePDU();
  switch (response) {
    case Ignore :
      return FALSE;

    case Confirm :
      if (confirm != NULL)
        WritePDU(*confirm);
      return FALSE;

    case Reject :
      if (reject != NULL)
        WritePDU(*reject);
      return FALSE;
  }

  H323TransactionPDU * rip = CreateRIP(request->GetSequenceNumber(), response);
  PBoolean ok = WritePDU(*rip);
  delete rip;

  if (!ok)
    return FALSE;

  if (fastResponseRequired) {
    fastResponseRequired = FALSE;
    PThread_H323::Create(PCREATE_NOTIFIER(SlowHandler), 0,
                                     PThread_H323::AutoDeleteThread,
                                     PThread_H323::NormalPriority,
                                     "Transaction:%x");
  }

  return TRUE;
}
PThread_H323 * PThread_H323::Create(const PNotifier & notifier,
                          INT parameter,
                          AutoDeleteFlag deletion,
                          Priority priorityLevel,
                          const PString & threadName,
                          PINDEX stackSize)
{
  PThread_H323 * thread = new PSimpleThread(notifier,
                                       parameter,
                                       deletion,
                                       priorityLevel,
                                       threadName,
                                       stackSize);
  if (deletion != AutoDeleteThread)
    return thread;

  // Do not return a pointer to the thread if it is auto-delete as this
  // pointer is extremely dangerous to use, it could be deleted at any moment
  // from now on so using the pointer could crash the program.
  return NULL;
}
```×
```c
PSimpleThread::PSimpleThread(const PNotifier & notifier,
                             INT param,
                             AutoDeleteFlag deletion,
                             Priority priorityLevel,
                             const PString & threadName,
                             PINDEX stackSize)
  : PThread_H323(stackSize, deletion, priorityLevel, threadName),
    callback(notifier),
    parameter(param)
{
  Resume();
}


void PSimpleThread::Main()
{
  callback(*this, parameter);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值