queue implementation with optional flag for triggering circular buffer mode