zoukankan      html  css  js  c++  java
  • Operating System: Three Easy Pieces --- Pthread Locks (Note)

    The name that the POSIX library uses for a lock is a mutex, as it is used to provide mutual

    exclusion between threads, i.e., if one thread is in the critical section, it excludes the others

    from entering until it has completed the section. Thus, when you see the following POSIX

    threads code, you should understand that it is doing the same thing as above (we again use

    wrappers that check for errors upon lock and unlock):

    pthread_mutex_t lock  = PTHREAD_MUTEX_INITITALIZED;

    pthread_mutex_lock(&lock);

    balance = balance + 1;

    pthread_mutex_unlock(&lock);

    You might also notice here that the POSIX version passes a variable to lock and unlock, as we

    may be using different locks to protect different variables. Doing so can increase concurrency:

    instead of one big lock that is used any time any critical section is accessed (a coarse-gained

    locking strategy), one will often protect different data and data structures with different locks,

    thus allowing more threads to be in locked code at once (a more fine-grained approach).

  • 相关阅读:
    zoj 3599 Game 博弈论
    hdu 2486/2580 / poj 3922 A simple stone game 博弈论
    hdu 1517 A Multiplication Game 博弈论
    hdu 4407 Sum 容斥原理
    hdu 4686 Arc of Dream
    hdu 4588 Count The Carries
    hdu 4586 Play the Dice
    C#学习(5)
    C#学习(4)
    C#学习(3)
  • 原文地址:https://www.cnblogs.com/miaoyong/p/4910796.html
Copyright © 2011-2022 走看看