simple ref count in CC++

    技术2025-11-03  6

    ref count is a very useful tech in managing resource in c/c++

     

    simply in c we can use the following code to implement ref count

     

    UPSINFO *attach_ups(UPSINFO *ups) { if (!ups) return new_ups(); P(ups->mutex); ups->refcnt++; V(ups->mutex); return ups; } void detach_ups(UPSINFO *ups) { P(ups->mutex); ups->refcnt--; if (ups->refcnt == 0) { destroy_ups(ups); } } void destroy_ups(UPSINFO *ups) { if (ups->refcnt == 0) { free(ups); } pthread_mutex_destroy(&ups->mutex); }

    最新回复(0)