c語言與函數式編程1(wrapper function)

一、c語言與函數式編程模式(funcitonal programming)

1)c語言通過函數指針(函數指針可以作爲參數,也可以作爲返回值)對funcitonal programming提供一定的支持

2)但又遠不夠強大,本身不支持閉包,嵌套定義等,遠未達到funcitonal programming中first class function(high order function)的境界


二、c語言標準中對函數指針的闡釋
1)函數指針可以進行類型轉換,也就是從一種函數指針類型轉換成另一種

2)但不支持函數指針非兼容類型(兩個函數指針具有不同的返回值等)的調用,可能會破壞棧


三、舉例說明(wrapper function爲例)
1)TaskCreate創建一個線程,入口函數爲entryPoint,由於entryPoint的函數類型和pthread_create中的函數類型不同
2)若直接進行轉換可能會引入上文提到的風險
3) 所以封裝一entry_wrapper函數,同時將entryPoint作爲線程入口函數參數傳遞給pthread_create


源碼如下:
void *entry_wrapper (void (*entryPoint)(void))//ljc define a wrapper funciton,(tycast different signature function pointer is ok,but call it use another type may corrupt stack)
{
  //entryPoint();
entryPoint();
return NULL;

}


int TaskCreate(UINT8 sName[], UINT32 dStackSize, void (*entryPoint)(void),INT32 dPriority, void *pPara, UINT32 *pTaskId)
{
  
  pthread_t thread;
pthread_attr_t attr;
struct sched_param param_sched;


param_sched.sched_priority=map_range_Osp(dPriority, 0, 255, 1, 99);

if(dStackSize<PTHREAD_STACK_MIN)
dStackSize=PTHREAD_STACK_MIN;
if(pthread_attr_init(&attr)!=0)
return -1;
printf(" %s %d %d\n",__FUNCTION__,__LINE__,PTHREAD_STACK_MIN);
printf("  %d\n",pthread_attr_setstacksize(&attr,(size_t)dStackSize));
printf("  %d\n",pthread_attr_setschedpolicy(&attr,SCHED_RR));
printf("  %d\n",pthread_attr_setschedparam(&attr,&param_sched));
if((pthread_attr_setstacksize(&attr,(size_t)dStackSize)!=0)||(pthread_attr_setschedpolicy(&attr,SCHED_RR)!=0)||(pthread_attr_setschedparam(&attr,&param_sched)!=0))
return -1;
printf(" %s %d\n",__FUNCTION__,__LINE__);
// if(pthread_create(&thread, &attr,(void *(*) (void *))entryPoint,pPara)!=0)//ljc when startroutine over,it will auto call pthread_exit;take startroutine's return value as it's exit status
  if(pthread_create(&thread, &attr,(void *(*) (void *))entry_wrapper,entryPoint)!=0)
  return -1;
if( pthread_attr_destroy(&attr)!=0)
return -1;
  if(pTaskId)
*pTaskId=(UINT32)thread;
return 0;
}



四、高階函數(high order function)
1)entry_wrapper本身有侷限,若TaskCreate的定義如下:
int TaskCreate(UINT8 sName[], UINT32 dStackSize, void (*entryPoint)(void*),INT32 dPriority, void *pPara, UINT32 *pTaskId)

也就是pPara不爲空,那例子中pPara就不能用來傳遞entryPoint,那上文中的entry_wrapper就無法正常工作了


2)解決此問題的策略,可用高階函數(接收一個函數作爲參數,並返回一個新的函數指針),高階函數的定義需滿足下述條件之一
函數作爲另一個函數的input,也就是參數
函數作爲另一個函數的output,也就是返回值
高階函數的 lua代碼示例:
    function newCounter ()
      local i = 0
      return function ()   -- anonymous function
               i = i + 1
               return i
             end
    end

3)高階函數的實現,由於c語言本身的侷限,實現起來相對複雜,後續再進一步說明
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章