Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 1.22 KB

README.md

File metadata and controls

31 lines (24 loc) · 1.22 KB

C-Lambda-and-Callback

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
);

Compilation

  • gcc -lpthread ./pthread.c -o ./pthread
  • gcc ./lambda.c -o ./lambda

Usage and Description

Please reference to my blog

Super Fast Ring Buffer

This is my version of Ring Buffer modified from Philip Thrasher I made macros deal with only pointer type for easier to use.