05 Concurrency and Race Conditions .

来源:互联网 发布:php字符串分割成数组 编辑:程序博客网 时间:2024/05/23 11:37

//spinlock #include <linux/spinlock.h>spinlock_t mylock = SPIN_LOCK_UNLOCKED; /* Initialize *//* Acquire the spinlock. This is inexpensive if there* is no one inside the critical section. In the face of* contention, spinlock() has to busy-wait.*/spin_lock(&mylock);/* ... Critical Section code ... */spin_unlock(&mylock); /* Release the lock *///Basic mutex usage is as follows:#include <linux/mutex.h>/* Statically declare a mutex. To dynamically   create a mutex, use mutex_init() */static DEFINE_MUTEX(mymutex);/* Acquire the mutex. This is inexpensive if there * is no one inside the critical section. In the face of * contention, mutex_lock() puts the calling thread to sleep. */mutex_lock(&mymutex);/* ... Critical Section code ... */mutex_unlock(&mymutex);      /* Release the mutex *///Basic usage of the semaphore interface is as follows:#include <asm/semaphore.h>  /* Architecture dependent                               header *//* Statically declare a semaphore. To dynamically   create a semaphore, use init_MUTEX() */static DECLARE_MUTEX(mysem);down(&mysem);    /* Acquire the semaphore *//* ... Critical Section code ... */up(&mysem);      /* Release the semaphore */

http://www.embexperts.com/viewthread.php?tid=31