PTHREAD_MUTEX_INITIALIZER vs pthread_mutex_init ( &mutex, param)

前端 未结 4 683
庸人自扰
庸人自扰 2020-12-07 14:55

Is there any difference between

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

Or

pthread_mutex_t lock         


        
4条回答
  •  粉色の甜心
    2020-12-07 15:34

    I would like to quote this from this book:

    With POSIX threads, there are two ways to initialize locks. One way to do this is to use PTHREAD_MUTEX_INITIALIZER, as follows: pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

    Doing so sets the lock to the default values and thus makes the lock usable. The dynamic way to do it (i.e., at run time) is to make a call to pthread_mutex_init() as follows: int rc = pthread_mutex_init(&lock, NULL); assert(rc == 0); // always check success!

    The first argument to this routine is the address of the lock itself, whereas the second is an optional set of attributes. Read more about the attributes yourself; passing NULL in simply uses the defaults. Either way works, but we usually use the dynamic (latter) method.

提交回复
热议问题