自旋锁是一种多线程同步机制,旨在保护共享资源免受并发访问的影响。在多个线程尝试获取锁时,它们会持续在循环中自旋(即不断检查锁是否可用),而不是立即进入休眠状态等待锁的释放。这种方法减少了线程切换的开销,适合于短时间内锁的竞争情况。然而,不恰当的使用可能会导致cpu资源的浪费。
自旋锁通常使用一个共享的标志位(例如一个布尔值)来表示锁的状态。当标志位为true时,表示锁已被某个线程占用;当标志位为false时,表示锁可用。当一个线程尝试获取自旋锁时,它会不断检查标志位:
优点:
缺点:
int pthread_spin_lock(pthread_spinlock_t *lock); int pthread_spin_trylock(pthread_spinlock_t *lock); int pthread_spin_unlock(pthread_spinlock_t *lock); int pthread_spin_init(pthread_spinlock_t *lock, int pshared); int pthread_spin_destroy(pthread_spinlock_t *lock);
自旋锁是一种适用于短时间内锁竞争情况的同步机制,它通过减少线程切换的开销来提高锁操作的效率。然而,它也存在CPU资源浪费和可能引起活锁等缺点。在使用自旋锁时,需要根据具体的应用场景进行选择,并确保锁被释放的时间尽可能短。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> int ticket = 1000; pthread_spinlock_t lock; void *route(void *arg) { char *id = (char *)arg; while (1) { pthread_spin_lock(&lock); if (ticket > 0) { usleep(1000); printf("%s sells ticket:%d\n", id, ticket); ticket--; pthread_spin_unlock(&lock); } else { pthread_spin_unlock(&lock); break; } } return NULL; } int main(void) { pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE); pthread_t t1, t2, t3, t4; pthread_create(&t1, NULL, route, (void *)"thread 1"); pthread_create(&t2, NULL, route, (void *)"thread 2"); pthread_create(&t3, NULL, route, (void *)"thread 3"); pthread_create(&t4, NULL, route, (void *)"thread 4"); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_join(t4, NULL); pthread_spin_destroy(&lock); return 0; }
在编写多线程程序时,常见一种情况是公共数据的修改机会较少,而读取的机会则相对较多。通常,读取过程伴随着查找操作,耗时较长。如果对这种代码段加锁,会极大地降低程序的效率。针对这种多读少写的情况,有一种专门的处理方法,即读写锁。
pthread库为我们提供了读写锁。
#include <iostream> #include <pthread.h> #include <unistd.h> #include <vector> #include <cstdlib> #include <ctime> // 共享资源 int shared_data = 0; // 读写锁 pthread_rwlock_t rwlock; // 读者线程函数 void *Reader(void *arg) { int number = *(int *)arg; while (true) { pthread_rwlock_rdlock(&rwlock); // 读者加锁 std::cout << "Reader " << number << " is reading. Shared data: " << shared_data << std::endl; pthread_rwlock_unlock(&rwlock); // 读者解锁 sleep(1); // 模拟读取时间 } return NULL; } // 写者线程函数 void *Writer(void *arg) { int number = *(int *)arg; while (true) { pthread_rwlock_wrlock(&rwlock); // 写者加锁 shared_data++; std::cout << "Writer " << number << " is writing. Shared data: " << shared_data << std::endl; pthread_rwlock_unlock(&rwlock); // 写者解锁 sleep(1); // 模拟写入时间 } return NULL; } int main() { pthread_rwlock_init(&rwlock, NULL); std::vector<pthread_t> threads; int num_readers = 5; int num_writers = 2; for (int i = 0; i < num_readers; i++) { int *arg = new int(i); pthread_t thread; pthread_create(&thread, NULL, Reader, arg); threads.push_back(thread); } for (int i = 0; i < num_writers; i++) { int *arg = new int(i); pthread_t thread; pthread_create(&thread, NULL, Writer, arg); threads.push_back(thread); } for (auto thread : threads) { pthread_join(thread, NULL); } pthread_rwlock_destroy(&rwlock); return 0; }
在这种策略中,系统会尽可能多地允许多个读者同时访问资源(比如共享文件或数据),而不会优先考虑写者。这意味着当有读者正在读取时,新到达的读者会立即被允许进入读取区,而写者则会被阻塞,直到所有读者都离开读取区。读者优先策略可能会导致写者饥饿(即写者长时间无法获得写入权限),特别是当读者频繁到达时。
在这种策略中,系统会优先考虑写者。当写者请求写入权限时,系统会尽快地让写者进入写入区,即使此时有读者正在读取。这通常意味着一旦有写者到达,所有后续的读者都会被阻塞,直到写者完成写入并离开写入区。写者优先策略可以减少写者等待的时间,但可能会导致读者饥饿(即读者长时间无法获得读取权限),特别是当写者频繁到达时。
以上就是【Linux】多线程(自旋锁、读写锁)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号