//callback void *func(void *arg){ int *pcount = (int *) arg; int i = 0; while (i++ < 100000) { (*pcount)++; usleep(1); } }
intmain(){ pthread_t th_id[THREAD_SIZE] = {0};
int i = 0; int count = 0; for (i = 0; i < THREAD_SIZE; i++) { int ret = pthread_create(&th_id[i], NULL, func, &count); if (ret) { break; } } for (i = 0; i < 100; i++) { printf("count --> %d\n", count); sleep(2); }
int i = 0; int count = 0; for (i = 0; i < THREAD_SIZE; i++) { int ret = pthread_create(&th_id[i], NULL, func, &count); if (ret) { break; } } for (i = 0; i < 100; i++) { printf("count --> %d\n", count); sleep(2); }
int i = 0; int count = 0; for (i = 0; i < THREAD_SIZE; i++) { int ret = pthread_create(&th_id[i], NULL, func, &count); if (ret) { break; } } for (i = 0; i < 100; i++) { pthread_rwlock_rdlock(&rwlock); printf("count --> %d\n", count); pthread_rwlock_unlock(&rwlock); sleep(2); }
int i = 0; int count = 0; for (i = 0; i < THREAD_SIZE; i++) { int ret = pthread_create(&th_id[i], NULL, func, &count); if (ret) { break; } } for (i = 0; i < 100; i++) { printf("count --> %d\n", count); sleep(2); }
}
补充:自旋锁会不会死锁?
给出我的观点:是锁就会有死锁问题!
嵌套自旋锁导致的死锁
1 2 3 4 5 6 7 8 9 10
spinlock_t lock;
voidthread_function() { spin_lock(&lock); // 第一次获取锁 // ... do some work ... spin_lock(&lock); // 再次尝试获取同一个锁,会导致死锁 // ... do some more work ... spin_unlock(&lock); spin_unlock(&lock); }
循环依赖导致的死锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
spinlock_t lock1, lock2;
voidthread_A() { spin_lock(&lock1); // ... do some work ... spin_lock(&lock2); // 线程A等待lock2 // ... do some more work ... spin_unlock(&lock2); spin_unlock(&lock1); }
voidthread_B() { spin_lock(&lock2); // ... do some work ... spin_lock(&lock1); // 线程B等待lock1 // ... do some more work ... spin_unlock(&lock1); spin_unlock(&lock2); }
intinc(int *value, int add){ int old; __asm__ volatile( "lock; xaddl %2, %1;" : "=a" (old) : "m" (*value), "a" (add) : "cc", "memory" ); return old; }
//callback void *func(void *arg){ int *pcount = (int *) arg; int i = 0; while (i++ < 100000) { inc(pcount, 1);
usleep(1); }
}
intmain(){ pthread_t th_id[THREAD_SIZE] = {0};
int i = 0; int count = 0; for (i = 0; i < THREAD_SIZE; i++) { int ret = pthread_create(&th_id[i], NULL, func, &count); if (ret) { break; } } for (i = 0; i < 100; i++) { printf("count --> %d\n", count); sleep(2); }
}
cmpxchg-----> CAS CAS:Compare And Swap,先比较,再赋值,翻译成代码就是下面
// Perform atomic 'compare and swap' operation on the pointer. // The pointer is compared to 'cmp' argument and if they are // equal, its value is set to 'val'. Old value of the pointer is returned. inline T *cas(T *cmp_, T *val_) { T *old; __asm__ volatile( "lock; cmpxchg %2, %3" : "=a" (old), "=m" (ptr) : "r" (val_), "m" (ptr), "0" (cmp_) : "cc"); return old; }
/* Initialize read-write lock RWLOCK using attributes ATTR, or use the default values if later is NULL. */ externintpthread_rwlock_init(pthread_rwlock_t *__restrict __rwlock, constpthread_rwlockattr_t *__restrict __attr) __THROW __nonnull((1));
/* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can be shared between different processes. */ externintpthread_spin_init(pthread_spinlock_t *__lock, int __pshared) __THROW __nonnull((1));
/* Destroy the spinlock LOCK. */ externintpthread_spin_destroy(pthread_spinlock_t *__lock) __THROW __nonnull((1));
/* Wait until spinlock LOCK is retrieved. */ externintpthread_spin_lock(pthread_spinlock_t *__lock) __THROWNL __nonnull((1));