当前位置: 首页 > 工具软件 > OpenH323 > 使用案例 >

OpenH323中线程创建的方式

林冥夜
2023-12-01

在分析线程模型时需要注意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);
}
 类似资料: