线程调用例子

typedef struct MacRangeT
{
    u8b startingCnuMac[6];
    u8b endingCnuMac[6];
}__attribute__((packed))MacRangeT;
typedef struct CMC_SetAddOrRemoveCnusReqT
{
    u8b groupId;
    u8b cnuOption;
    u8b numOfEntries;
    MacRangeT CnuMac[201];
}__attribute__((packed))CMC_SetAddOrRemoveCnusReqT;

//线程处理函数

void * SetAddCNUsToLdBalGrp_start_run(void * arg)
{
CMC_SetAddOrRemoveCnusReqT *pRevBuf = (CMC_SetAddOrRemoveCnusReqT *)arg;
    u16b reqOff = 0;
u8b resultCode = OK;
cout << "SetAddCNUsToLdBalGrp_start_run "  << endl;

    // Get the load balancing ID from the request buffer.
    u8b loadBalancingGroupID = pRevBuf->groupId;
    cout << "Load Balancing Group ID = 0x" << hex << (int) loadBalancingGroupID << endl;

    // Get the CNU option from the request buffer.
    // 1=Add, 2=Delete
    u8b cnuOption = pRevBuf->cnuOption;
    cout << "CNU option = 0x" << hex << (int) cnuOption << endl;
    if (cnuOption > 2)
    {
        cout << "Invalid CNU option parameter!" << endl;
        resultCode = INVALID_PARMS;
    }

u8b numOfEntries = pRevBuf->numOfEntries;
cout << "Invalid CNU numOfEntries:" << (int) numOfEntries << endl;
u8b numOfmac = 0; 
for( numOfmac=0; numOfmac < numOfEntries; numOfmac++)
{
   if (resultCode == OK)
   {
       u8b startMacAddr[6] = {0};
       u8b endMacAddr[6] = {0};
memcpy(startMacAddr,pRevBuf->CnuMac[numOfmac].startingCnuMac,6);
memcpy(endMacAddr,pRevBuf->CnuMac[numOfmac].endingCnuMac,6);

       // FIgure out which method is being specified by the user.
       cout << "CNU specification method detected = ";
       u8b cnuMethod = LB_RANGEMACS;
       u8b ouiBytes[3] = {0, 0, 0};
       if (memcmp(startMacAddr, endMacAddr, 6) == 0)
       {
           cnuMethod = LB_SINGLEMAC;
           cout << "SingleMac" << endl;
       }
       else if ((memcmp(startMacAddr, endMacAddr, 3) == 0) &&
                (memcmp(startMacAddr+3, ouiBytes, 3) == 0) &&
                (memcmp(endMacAddr+3, ouiBytes, 3) == 0))
       {
           cnuMethod = LB_OUI;
           cout << "OUI" << endl;
       }
       else
       {
           cout << "RangeOfMacs" << endl;
           if (memcmp(startMacAddr, endMacAddr, 6) > 0)
           {
               cout << "ERROR: Starting MAC address is greater than ending MAC address!" << endl;
               cout << "Not adding CNU MAC address range to load balancing group!" << endl;
               resultCode = ERROR;
           }
       }

       if (resultCode == OK)
       {
           switch (cnuMethod)
           {
               case LB_SINGLEMAC:
                   {
                       BcmMacAddress startMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
                                              startMacAddr[3], startMacAddr[4], startMacAddr[5]);
                       if (cnuOption == LB_CREATE)
                       {
                           resultCode = theLoadBalancer().AddMacAddressToGroup(startMac, loadBalancingGroupID);
                       }
                       else if (cnuOption == LB_DESTROY)
                       {
                           resultCode = theLoadBalancer().DropMacAddressFromGroup(startMac, loadBalancingGroupID);
                       }
                   }
                   break;
               case LB_OUI:
                   {
                       BcmMacAddress ouiMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
                                            startMacAddr[3], startMacAddr[4], startMacAddr[5]);
                       if (cnuOption == LB_CREATE)
                       {
                           resultCode = theLoadBalancer().AddOuiToGroup(ouiMac, loadBalancingGroupID);
                       }
                       else if (cnuOption == LB_DESTROY)
                       {
                           resultCode = theLoadBalancer().DropOuiFromGroup(ouiMac, loadBalancingGroupID);
                       }
                   }
                   break;
               case LB_RANGEMACS:
                   {
                       BcmMacAddress startMac(startMacAddr[0], startMacAddr[1], startMacAddr[2],
                                              startMacAddr[3], startMacAddr[4], startMacAddr[5]);
                       BcmMacAddress endMac(endMacAddr[0], endMacAddr[1], endMacAddr[2],
                                            endMacAddr[3], endMacAddr[4], endMacAddr[5]);
                       if (cnuOption == LB_CREATE)
                       {
                           resultCode = theLoadBalancer().AddMacAddressRangeToGroup(startMac, endMac, loadBalancingGroupID);
                       }
                       else if (cnuOption == LB_DESTROY)
                       {
                           resultCode = theLoadBalancer().DropMacAddressRangeFromGroup(startMac, endMac, loadBalancingGroupID);
                       }
                   }
                   break;
               default:
                   resultCode = ERROR;
                   break;
           }
       }
   }
}

if ( numOfEntries > 1)
{
resultCode = theLoadBalancer().UpdateActiveCMByGroup(loadBalancingGroupID);
}

    return NULL;
}

//线程启动函数

u8b SetAddCNUsToLdBalGrpThread(CMC_SetAddOrRemoveCnusReqT *pRequestBuf)

{
int rc = 0;
struct sched_param priority_holder ;
pthread_attr_t attr;
pthread_t thread_id;

priority_holder.sched_priority = 71; //设置线程的优先级1-99,值越大优先级越高
pthread_attr_init(&attr);
pthread_attr_setschedparam(&attr,&priority_holder);//设置优先级
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//初始化线程属性
rc = pthread_create(&thread_id,&attr,SetAddCNUsToLdBalGrp_start_run,pRequestBuf);//启动线程SetAddCNUsToLdBalGrp_start_run调用线程处理函数,pRequestBuf传入值
if (rc)
{
cout<< "ERROR; return code is :"<< rc << endl;
return 1;

}

else
{
      pthread_detach(thread_id);
}

pthread_attr_destroy(&attr);
//pthread_exit(NULL);

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值