linux c++多线程 线程私有数据 互斥量 条件变量 信号量 读写锁 自旋锁 屏障
来源:互联网 发布:剑灵天女捏脸数据大全 编辑:程序博客网 时间:2024/05/02 03:09
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <pthread.h>#include <unistd.h>#include <queue>#include <iostream>#include <semaphore.h>using namespace std;//多线程//线程私有数据//互斥量//条件变量//信号量//读写锁//自旋锁//屏障pthread_key_t key;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t conc = PTHREAD_COND_INITIALIZER, conp = PTHREAD_COND_INITIALIZER;pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;pthread_spinlock_t spinlock;pthread_barrier_t barrier;sem_t sem1, sem2;queue<size_t> product;typedef void(*pf)(void*);void *thread1(void *arg){for (size_t i = 0; i < 10; i++){//sleep(1);sem_wait(&sem1);product.push(i);cout << "id:" << pthread_self() << "product:" << product.back() << endl;sem_post(&sem2);}}void *thread2(void *arg){for (size_t i = 0; i < 10; i++){//sleep(2);sem_wait(&sem2);cout << "id:" << pthread_self() << "consume:" << product.front() << endl;product.pop();sem_post(&sem1);}}void *thread3(void *arg){//sleep(1);//int a = 50;//pthread_setspecific(key, (void*)a);//pthread_getspecific(key);//printf("thread_function1...%d\n", pthread_getspecific(key));pthread_cleanup_push((pf)pthread_mutex_unlock, &mutex);for (size_t i = 0; i < 10; ++i){pthread_mutex_lock(&mutex);if (product.size() > 0){pthread_cond_wait(&conp, &mutex);}product.push(i);cout << "id:" << pthread_self() << "product:" << product.back() << endl;pthread_cond_signal(&conc);pthread_mutex_unlock(&mutex);}pthread_cleanup_pop(0);return NULL;}void *thread4(void *arg){//sleep(1);//const char* ss = "abcdefg";//pthread_setspecific(key, ss);//printf("thread_function2...%s\n", (char*)pthread_getspecific(key));pthread_cleanup_push((pf)pthread_mutex_unlock, &mutex);for (size_t i = 0; i < 10; ){pthread_mutex_lock(&mutex);if (product.empty()){//pthread_mutex_unlock(&mutex);//continue;pthread_cond_wait(&conc, &mutex);}++i;cout << "id:" << pthread_self() << "consume:" << product.front() << endl;product.pop();pthread_cond_signal(&conp);pthread_mutex_unlock(&mutex);}pthread_cleanup_pop(0);return NULL;}void *thread5(void *arg){for (size_t i = 0; i < 10; i++){sleep(1);pthread_rwlock_rdlock(&rwlock);cout << "id:" << pthread_self() << "read" << endl;pthread_rwlock_unlock(&rwlock);}}void *thread6(void *arg){for (size_t i = 0; i < 10; i++){sleep(2);pthread_rwlock_wrlock(&rwlock);cout << "id:" << pthread_self() << "write" << endl;pthread_rwlock_unlock(&rwlock);}}void *thread7(void *arg){for (size_t i = 0; i < 10; i++){sleep(1);pthread_spin_lock(&spinlock);cout << "id:" << pthread_self() << "spinlock1" << endl;pthread_spin_unlock(&spinlock);}}void *thread8(void *arg){for (size_t i = 0; i < 10; i++){sleep(1);pthread_spin_lock(&spinlock);cout << "id:" << pthread_self() << "spinlock2" << endl;pthread_spin_unlock(&spinlock);}}void *thread9(void *arg){for (size_t i = 0; i < 5; i++){sleep(1);cout << "id:" << pthread_self() << "barrier:" << i << endl;}pthread_barrier_wait(&barrier);}void *thread10(void *arg){for (size_t i = 0; i < 10; i++){sleep(1);cout << "id:" << pthread_self() << "barrier:" << i << endl;}pthread_barrier_wait(&barrier);}int main(){pthread_t tid1;pthread_t tid2;pthread_t tid3;pthread_t tid4;pthread_t tid5;pthread_t tid6;pthread_t tid7;pthread_t tid8;pthread_t tid9;pthread_t tid10;//keypthread_key_create(&key, NULL);sem_init(&sem1, 0, 1);sem_init(&sem2, 0, 0);pthread_rwlock_init(&rwlock, NULL);pthread_spin_init(&spinlock, 0);pthread_barrier_init(&barrier, NULL, 3);//sempthread_create(&tid1, NULL, thread1, NULL);pthread_create(&tid2, NULL, thread2, NULL);//mutex & cond//pthread_create(&tid3, NULL, thread3, NULL);//pthread_create(&tid4, NULL, thread4, NULL);//rwlockpthread_create(&tid5, NULL, thread5, NULL);pthread_create(&tid6, NULL, thread6, NULL);//spinlockpthread_create(&tid7, NULL, thread7, NULL);pthread_create(&tid8, NULL, thread8, NULL);//barrierpthread_create(&tid9, NULL, thread9, NULL);pthread_create(&tid10, NULL, thread10, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);//pthread_join(tid3, NULL);//pthread_join(tid4, NULL);pthread_join(tid5, NULL);pthread_join(tid6, NULL);pthread_join(tid7, NULL);pthread_join(tid8, NULL);pthread_barrier_wait(&barrier);pthread_join(tid9, NULL);pthread_join(tid10, NULL);pthread_key_delete(key);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&conp);pthread_cond_destroy(&conc);sem_destroy(&sem1);sem_destroy(&sem2);pthread_rwlock_destroy(&rwlock);pthread_spin_destroy(&spinlock);pthread_barrier_destroy(&barrier); return 0;}
阅读全文
0 0
- linux c++多线程 线程私有数据 互斥量 条件变量 信号量 读写锁 自旋锁 屏障
- 线程同步机制(互斥量,读写锁,自旋锁,条件变量,屏障)
- linux线程,互斥量,读写锁,条件变量和屏障
- 多线程(互斥锁、读写锁、条件变量、信号量、自旋锁)相关
- 多线程(互斥锁、读写锁、条件变量、信号量、自旋锁)相关
- 第十一章 线程之锁、读写锁、条件变量、屏障
- 线程同步机制:互斥量、信号量、读写锁、条件变量
- linux下多线程同步机制之信号量、互斥量、读写锁、条件变量
- Linux——线程信号量 读写锁 线程的私有数据
- 线程同步:原子操作、锁、二元信号量、信号量、互斥量、临界区、读写锁、条件变量等
- 线程同步:原子操作、锁、二元信号量、信号量、互斥量、临界区、读写锁、条件变量
- 线程同步(互斥锁、读写锁、条件变量、信号量)
- 互斥锁 自旋锁 读写锁 条件变量
- linux中的互斥锁--mutex,条件变量,信号量,读写锁
- linux多线程-----同步机制(互斥量、读写锁、条件变量)
- Linux 线程锁,条件变量 信号量的区别
- Linux 线程锁 条件变量 信号量的区别2
- Linux 线程锁 条件变量 信号量的区别3
- 如何快速通过json构建javabean对象(Intellij IDEA-->GsonFormat使用教程)
- 进程之间的通信
- windos和linux 下遍历下级文件及文件夹
- opengles基本变换的实质
- 【DP】TEST20170518
- linux c++多线程 线程私有数据 互斥量 条件变量 信号量 读写锁 自旋锁 屏障
- 把数据库里的字段值分别加载
- 第一章 shell基本使用(一)
- bzoj 1264: [AHOI2006]基因匹配Match (DP+树状数组)
- android 观察者模式
- 递归函数
- Java基础练习题 (7)网络编程
- 第三天的感想
- 随手记