Lambda expression has been supported by many programing languages. But it is not supported by standard C. This problem is now solved by the combinations of some techniques. If you want to have the same functionality of lambda, your compiler needs to support nested function. The result would be something like this:
my_qsort(values, 6, a, b, return (*(int*)a - *(int*)b););
If you only needs a callback function, there's no extra requirements. it can be compiled by almost every C compiler. The result is like this:
pop_job(job_semaphore,
//In callback function
//This is for double check, preventing unexpeted bug
while(isBufferEmpty(buffer_t));
bufferRead(buffer_t, p);
printf("\tpop %d, %d\n", p.x, p.y); //Print message
);
- gcc -lpthread ./pthread.c -o ./pthread
- gcc ./lambda.c -o ./lambda
Please reference to my blog
This is my version of Ring Buffer modified from Philip Thrasher I made macros deal with only pointer type for easier to use.