Creating a Gearman Worker in C
I've been trying to implement a Gearman worker with their C API (libgearman). But their documentation for the C API is poor and not complete. Although its very similar to creating workers in other languages such as PHP, I am still unable to Register a function in the worker via *gearman_worker_add_function();*
To be specific, I am unable to find out how to create an object of *gearman_worker_fn* 开发者_JAVA百科.
Thanks!
According to this documentation:
typedef void*( gearman_worker_fn)(gearman_job_st *job, void *context, size_t *result_size, gearman_return_t *ret_ptr)
Definition at line 407 of file constants.h.
This is a function pointer typedef: Basically you just need to create a function with the correct signature (returns void *
, takes in gearman_job_st *
, void *
, size_t *
, gearman_return_t *
), and then pass in the function name.
Example:
void *do_work(gearman_job_st *job, void *context, size_t *result_size, gearman_return_t *ret_ptr)
{
do_something();
}
/* Later... */
gearman_worker_add_function(worker, function_name, timeout, do_work, context);
Notice that I just pass the function name-- this degrades to a "function pointer".
精彩评论