酒泉网站建设服务,dw设计网页步骤,企业logo设计思路,长丰网站建设目录
1、概念
2、线程的实现#xff1a;
3、线程同步#xff1a;
4、使用信号量#xff1a;
5、使用信号量实现进程同步#xff1a;
6、使用互斥锁
7、使用互斥锁实现线程同步
8、读写锁
9、使用读写锁
10、使用读写锁实现进程同步 1、概念
线程#xff1a;进程…目录
1、概念
2、线程的实现
3、线程同步
4、使用信号量
5、使用信号量实现进程同步
6、使用互斥锁
7、使用互斥锁实现线程同步
8、读写锁
9、使用读写锁
10、使用读写锁实现进程同步 1、概念
线程进程内部的一条执行路径
进程正在运行的程序
并发在一段时间内两者交替运行
并行在一段时间内两者同时执行必须有两个处理器
2、线程的实现
用户级线程模拟出来多条路径在内核眼里依旧是一条路径他的出现不需要操作系统参与可以创建特别多的线程开销小但是无法使用多个处理器只能并发
linux内核级线程通过内核提供的接口去创建线程创建开销大但是可以实现并行使用多个处理器
组合可以利用多处理器的资源在用户空间可以创建更多线程对于后期线程的创建来说开销小
在Linux系统上没有线程的概念每一个线程都是当作进程来实现的是一个和其他进程共享某些资源的进程都拥有属于自己的内存控制块
3、线程同步
信号量 互斥锁 条件变量 读写锁
对程序的执行进行控制保证程序的正确性多个线程按照一定顺序执行。
没有进行同步线程的程序
int g_count1;
void* fun(void *arv)
{
for(int i0;i1000;i)
{
printf(g_count%d,g_count);
}
}
int main()
{
pthread_t id[5];
for(int i0;i5;i)
{
pthread_creat(id[i],NULL,fun,NULL);
}
for(int i0;i5;i)
{
pthread_join(id[i],NULL);//等待其他线程结束
}
exit(0);
}
对于最终结果因为在运行过程中可能出现在同一时间两个线程对同一个数据进行操作的情况所以结果可能为5000也可能小于5000
4、使用信号量
头文件#includesemphore.h
初始化信号量sem_t sem;
执行p操作sem_wait(sem);
执行v操作:sem_post(sem);
销毁sem_destroy(sem);
5、使用信号量实现进程同步
使用了信号量进行线程同步
#includesemaphore.h
int g_count1;
sem_t sem;
void* fun(void *arv)
{
for(int i0;i1000;i)
{
sem_wait(sem);//p操作
printf(g_count%d,g_count);
sem_post(sem);//v操作
}
}
int main()
{
sem_init(sem,0,1);//sem1
//信号量初始化pthread_t id[5];
for(int i0;i5;i)
{
pthread_creat(id[i],NULL,fun,NULL);
}
for(int i0;i5;i)
{
pthread_join(id[i],NULL);//等待其他线程结束
}
sem_destroy(sem);//销毁信号量
exit(0);
}
//结果无论怎么执行都是5000
6、使用互斥锁
要不我用要不你有在使用之前先加锁使用时候再解锁其他人用再加锁再解锁
定义pthread_mutex_t mutex;
初始化pthread_mutex_init(mutex,NULL);
加锁pthread_mutex_lock(mutex);
解锁pthread_mutex_unlock(mutex);
销毁pthread_mutex_destroy(mutex);
7、使用互斥锁实现线程同步
使用互斥锁实现线程同步
int g_count1;pthread_mutex_t mutex;
//定义互斥锁
void* fun(void *arv)
{
for(int i0;i1000;i)
{
pthread_mutex_lock(mutex);
//加锁
printf(g_count%d,g_count);
pthread_mutex_unlock(mutex);
//解锁
}
}
int main()
{
pthread_mutex_init(mutex,NULL);
//互斥锁初始化
pthread_t id[5];
for(int i0;i5;i)
{
pthread_creat(id[i],NULL,fun,NULL);
}
for(int i0;i5;i)
{
pthread_join(id[i],NULL);//等待其他线程结束
}
exit(0);
pthread_mutex_destroy(mutex);
}
8、读写锁
读写锁允许多个线程同时读取一个资源但只允许一个线程对他进行修改
读写锁有两种状态读模式和写模式。多个线程可以同时持有读模式的锁以允许并发读取共享资源。
当有线程持有读模式锁时其他线程也可以继续获取读模式锁但不能获取写模式锁。只有当没有线程持有读模式锁时才能获取写模式锁进行写操作。
9、使用读写锁
定义pthread_rwlock_t lock;
初始化pthread_rwlock_init(lock,NULL);
写加锁pthread_rwlock_wrlock(lock);
写解锁pthread_rwlock_unlock(lock);
读加锁pthread_rwlock_rdlock(lock)
读解锁pthread_rwlock_unlock(lock)
销毁锁pthread_rwlock_destroy(lock)
10、使用读写锁实现进程同步
pthread_rwlock_t lock;void *pthread_fun(void*arg)
{
for(int i0;i10;i)
{
pthread_rwlock_wrlock(lock);
printf(w star);
sleep(1);
printf(w end);
pthread_rwlock_wrlock(lock);
sleep(1);
}
}void*pthread_fun1(void*arg)
{
for(int i0li10;i)
{
pthread_rwlock_rdlock(lock);
printf(r1 star);
sleep(1);
printf(r1 end);
pthread_rwlock_rdlock(lock);
}
}void*pthread_fun2(void*arg)
{
for(int i0;i5;i)
{
pthread_rwlock_rdlock(lock);
printf(r2 star);
sleep(1);
printf(r2 end);
pthread_rwlock_unlock(lock);
sleep(1);
}
}int main()
{
pthread_rwlock_init(lock,NULL);
pthread_t id1,id2,id3;
pthread_create(id1,NULL,pthread_fun1,NULL);
pthread_create(id2,NULL,pthread_fun2,NULL);
pthread_create(id3,NULL,pthread_fun,NULL);pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);pthread_rwlock_destroy(lock);
exit(0);
}