| 網(wǎng)站首頁(yè) | 關(guān)于我們 | 開(kāi)發(fā)優(yōu)勢(shì) | 產(chǎn)品展示 |
| 合作企業(yè) | 新聞動(dòng)態(tài) | 聯(lián)系我們 | 電話聯(lián)系 |
文章作者:濟(jì)南軟件開(kāi)發(fā) 時(shí)間:2016年12月20日
一、c語(yǔ)言與函數(shù)式編程模式(funcitonal programming)
1)c語(yǔ)言通過(guò)函數(shù)指針(函數(shù)指針可以作為參數(shù),也可以作為返回值)對(duì)funcitonal programming提供一定的支持
2)但又遠(yuǎn)不夠強(qiáng)大,本身不支持閉包,嵌套定義等,遠(yuǎn)未達(dá)到funcitonal programming中first class function(high order function)的境界
二、c語(yǔ)言標(biāo)準(zhǔn)中對(duì)函數(shù)指針的闡釋
1)函數(shù)指針可以進(jìn)行類(lèi)型轉(zhuǎn)換,也就是從一種函數(shù)指針類(lèi)型轉(zhuǎn)換成另一種
2)但不支持函數(shù)指針?lè)羌嫒蓊?lèi)型(兩個(gè)函數(shù)指針具有不同的返回值等)的調(diào)用,可能會(huì)破壞棧
三、舉例說(shuō)明(wrapper function為例)
1)TaskCreate創(chuàng)建一個(gè)線程,入口函數(shù)為entryPoint,由于entryPoint的函數(shù)類(lèi)型和pthread_create中的函數(shù)類(lèi)型不同
2)若直接進(jìn)行轉(zhuǎn)換可能會(huì)引入上文提到的風(fēng)險(xiǎn)
3) 所以封裝一entry_wrapper函數(shù),同時(shí)將entryPoint作為線程入口函數(shù)參數(shù)傳遞給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_priZ喎?"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcml0eT1tYXBfcmFuZ2VfT3NwKGRQcmlvcml0eSwgMCwgMjU1LCAxLCA5OSk7PGJyPgo8YnI+CmlmKGRTdGFja1NpemU8UFRIUkVBRF9TVEFDS19NSU4pPGJyPgpkU3RhY2tTaXplPVBUSFJFQURfU1RBQ0tfTUlOOzxicj4KaWYocHRocmVhZF9hdHRyX2luaXQoJmFtcDthdHRyKSE9MCk8YnI+CnJldHVybiAtMTs8YnI+CnByaW50Zig=" %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,¶m_sched));
if((pthread_attr_setstacksize(&attr,(size_t)dStackSize)!=0)||(pthread_attr_setschedpolicy(&attr,SCHED_RR)!=0)||(pthread_attr_setschedparam(&attr,¶m_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;
}
四、高階函數(shù)(high order function)
1)entry_wrapper本身有局限,若TaskCreate的定義如下:
int TaskCreate(UINT8 sName[], UINT32 dStackSize, void (*entryPoint)(void*),INT32 dPriority, void *pPara, UINT32 *pTaskId)
也就是pPara不為空,那例子中pPara就不能用來(lái)傳遞entryPoint,那上文中的entry_wrapper就無(wú)法正常工作了
2)解決此問(wèn)題的策略,可用高階函數(shù)(接收一個(gè)函數(shù)作為參數(shù),并返回一個(gè)新的函數(shù)指針),高階函數(shù)的定義需滿足下述條件之一
函數(shù)作為另一個(gè)函數(shù)的input,也就是參數(shù)
函數(shù)作為另一個(gè)函數(shù)的output,也就是返回值
高階函數(shù)的 lua代碼示例:
function newCounter ()
local i = 0
return function () -- anonymous function
i = i + 1
return i
end
end
想要了解更多詳情歡迎來(lái)電咨詢(xún)18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。