How can I create a smart pointer that locks and unlocks a mutex?

前端 未结 3 466
迷失自我
迷失自我 2021-01-01 19:51

I have a threaded class from which I would like to occasionally acquire a pointer an instance variable. I would like this access to be guarded by a mutex so that the thread

3条回答
  •  一整个雨季
    2021-01-01 20:32

    You're describing a variation of the EXECUTE AROUND POINTER pattern, described by Kevlin Henney in Executing Around Sequences.

    I have a prototype implementation at exec_around.h but I can't guarantee it works correctly in all cases as it's a work in progress. It includes a function mutex_around which creates an object and wraps it in a smart pointer that locks and unlocks a mutex when accessed.

提交回复
热议问题