i have rather complex c99 conform shared library. contains large number of global variables , many short functions, few used user directly.
i'd refactor thread-safe library. that, make user pass handle each function. handle pointer struct contains global variables.
performance important in case , don't want jeopardize it. need careful when implementing handles? example, typical function looks this
void calculate(){ (int i=0; i<n; i++){ particle[i].x += g * particle[i].y; other_function_that_does_something_to_particle(); } } naively, add handle this
void calculate(struct handle* h){ (int i=0; i<h->n; i++){ h->particle[i].x += h->g * h->particle[i].y; other_function_that_does_something_to_particle(h); } } which looks rather inefficient. there better way?
the cost of passing pointers typically marginal. has higher impact on architectures, parameters passes through stack , smaller when parameters passed through registers.
use const pointers (pointers const data). use volatile field or pointer specifiers when data can modified other threads. , use restrict.
all type specifiers have impact. example, when processing multiple references without restrict, compiler might forced re-read values if has read them. , on.
worth read: https://en.wikipedia.org/wiki/restrict
Comments
Post a Comment