#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;
//key
pthread_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);
//sem
pthread_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);
//rwlock
pthread_create(&tid5, NULL, thread5, NULL);
pthread_create(&tid6, NULL, thread6, NULL);
//spinlock
pthread_create(&tid7, NULL, thread7, NULL);
pthread_create(&tid8, NULL, thread8, NULL);
//barrier
pthread_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;
}
linux c++多线程 线程私有数据 互斥量 条件变量 信号量 读写锁 自旋锁 屏障
最新推荐文章于 2023-03-10 17:17:24 发布