I. C language and functional programming model (funcitonal programming)
1) the C language provides support for funcitonal programming through function pointers (function pointers can be used as parameters or return values ).
2) but it is far from powerful enough. It does not support closures and nested definitions. It is far from reaching the realm of the first class function (high order function) in funcitonal programming.
Ii. Explanation of function pointers in C Language Standards
1) function pointers can be converted to another type, that is, from one type of function pointer to another type.
2) but does not support calls of incompatible function pointers (two function pointers have different return values), which may damage the stack.
Iii. Example (wrapper function)
1) TaskCreate a thread and the entry function is entryPoint. The function type of entryPoint is different from that of pthread_create.
2) directly performing conversions may introduce the risks mentioned above.
3) encapsulate an entry_wrapper function and pass entryPoint to pthread_create as the thread entry function parameter.
The source code is as follows:
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 have upt stack)
// EntryPoint ();
Int TaskCreate (UINT8 sName , UINT32 dStackSize, void (* entryPoint) (void), INT32 dPriority, void * pPara, UINT32 * pTaskId)
Struct sched_param param_sched;
Param_sched.sched_priZ shard? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> cipher + CnJldHVybiAtMTs8YnI + CnByaW50Zig =" % s % 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 ))
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)
If (pthread_attr_destroy (& attr )! = 0)
* PTaskId = (UINT32) thread;
4. high order function)
1) entry_wrapper has limitations. If TaskCreate is defined as follows:
Int TaskCreate (UINT8 sName , UINT32 dStackSize, void (* entryPoint) (void *), INT32 dPriority, void * pPara, UINT32 * pTaskId)
That is, if pPara is not empty, pPara in this example cannot be used to pass entryPoint, and the above entry_wrapper will not work properly.
2) the policy for solving this problem can be used for higher-order functions (receiving a function as a parameter and returning a new function pointer). The definition of higher-order functions must meet one of the following conditions:
Function as the input of another function, that is, the parameter
Function as the output of another function, that is, the return value
Lua code example for higher-order functions:
Function newCounter ()
Local I = 0
Return function () -- anonymous function
I = I + 1
3) The implementation of higher-order functions is relatively complicated due to the limitations of the C language itself, and will be further explained later.