Overhead of pthread mutexes?

前端 未结 9 1846
旧时难觅i
旧时难觅i 2020-12-13 18:46

I\'m trying to make a C++ API (for Linux and Solaris) thread-safe, so that its functions can be called from different threads without breaking internal data structures. In m

9条回答
  •  时光取名叫无心
    2020-12-13 19:19

    Well a suboptimal but simple approach is to place macros around your mutex locks and unlocks. Then have a compiler / makefile option to enable / disable threading.

    Ex.

    #ifdef THREAD_ENABLED
    #define pthread_mutex_lock(x) ... //actual mutex call
    #endif
    
    #ifndef THREAD_ENABLED
    #define pthread_mutex_lock(x) ... //do nothing
    #endif
    

    Then when compiling do a gcc -DTHREAD_ENABLED to enable threading.

    Again I would NOT use this method in any large project. But only if you want something fairly simple.

提交回复
热议问题