当前位置: 首页 > news >正文

德州做网站seo基本概念

德州做网站,seo基本概念,深圳排名网站,英国有哪些做折扣的网站文章目录 线程同步概念条件变量使用生产消费模型信号量的使用读写锁的使用 Linux 线程安全 (1) 线程同步概念 竞态条件:因为时序问题,而导致程序异常. 饥饿问题:只使用互相锁保证线程安全时,锁资源总被某…

文章目录

  • 线程同步概念
  • 条件变量使用
  • 生产消费模型
  • 信号量的使用
  • 读写锁的使用

Linux 线程安全 (1)

线程同步概念

竞态条件:因为时序问题,而导致程序异常.

饥饿问题:只使用互相锁保证线程安全时,锁资源总被某一个线程占用的情况。

线程同步:线程同步是指在多线程编程中,为了保证临界资源的正确访问和避免竞态条件,需要协调和控制线程之间的执行顺序和互斥访问。让线程能够按照某种特定的顺序访问临界资源,从而有效避免饥饿问题。常见的线程同步机制包括互斥锁(Mutex)、信号量(Semaphore)、条件变量(Condition Variable)等。

条件变量使用

在多线程编程中,有需要等待某个资源就绪,线程才能开始正常运行的需求。
例如一个线程访问队列时,发现队列为空,它只能等待,只到其它线程将一个节点添加到队列中。这种情况就需要用到条件变量。

条件变量是一种线程同步的高级机制,它可以实现线程的等待和唤醒操作。条件变量通常与互斥锁一起使用,当某个条件不满足时,线程可以调用条件变量的等待操作进入等待状态,当条件满足时,其他线程可以调用条件变量的唤醒操作来唤醒等待的线程。

// 1.初始化条件变量 
//cond为要初始化的条件变量
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
//2.等待条件满足 
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
//3.唤醒等待
//唤醒所有在等待的线程
int pthread_cond_broadcast(pthread_cond_t *cond);
//随机唤醒一个真在等待的线程
int pthread_cond_signal(pthread_cond_t *cond);

例子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>pthread_cond_t cond;
pthread_mutex_t mutex;void *r1( void *arg )
{while ( 1 ){pthread_cond_wait(&cond, &mutex);printf("活动\n");}
}void *r2(void *arg )
{while ( 1 ) {pthread_cond_signal(&cond);sleep(1);}
}int main( void )
{pthread_t t1, t2;pthread_cond_init(&cond, NULL);pthread_mutex_init(&mutex, NULL);pthread_create(&t1, NULL, r1, NULL);pthread_create(&t2, NULL, r2, NULL);pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);
}

条件变量实际使用规范
①等待条件代码

pthread_mutex_lock(&mutex);
while (条件为假)
pthread_cond_wait(cond, mutex);
修改条件
pthread_mutex_unlock(&mutex);

②给条件发送信号代码

pthread_mutex_lock(&mutex);
设置条件为真
pthread_cond_signal(cond);
pthread_mutex_unlock(&mutex);

"条件” 可以理解为需要等待就绪的资源。
条件为假:线程代码运行需要的资源未就绪。
条件为真:线程代码运行需要的资源就绪。
修改条件:使用资源。

生产消费模型

产者消费者模型是一种常见的并发编程模型,用于解决多线程环境下的生产者和消费者之间的协作问题。在这个模型中,生产者负责生产数据,并将数据放入共享的缓冲区中,而消费者则负责从缓冲区中取出数据并进行消费。

该模型的基本思想是通过一个共享的缓冲区来实现生产者和消费者之间的同步与通信。生产者在生产数据之前会检查缓冲区是否已满,如果已满则等待,直到有空闲位置。而消费者在消费数据之前会检查缓冲区是否为空,如果为空则等待,直到有数据可供消费。

为了实现这种同步与通信,可以使用互斥锁(mutex)来保护缓冲区的访问,以及条件变量(condition variable)来实现生产者和消费者之间的等待和通知机制。

在这里插入图片描述

信号量的使用

此处的信号量使用指的是对POSIX信号量的使用,POSIX信号量可以用于线程间同步。

//1.初始化信号量
//pshared=0表示线程间共享信号量
//value 表示信号量的初始值
int sem_init(sem_t *sem, int pshared, unsigned int value);
//2.等待信号量,会将信号量的值减
int sem_wait(sem_t *sem);
//3.发布信号量,表示资源使用完毕,可以归还资源了。将信号量值加1
int sem_post(sem_t *sem);
//4.销毁信号量
int sem_destroy(sem_t *sem);
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>#define NUM_THREADS 5sem_t semaphore;void* thread_function(void* arg) 
{int thread_id = *(int*)arg;printf("Thread %d is waiting...\n", thread_id);sem_wait(&semaphore);printf("Thread %d has acquired the semaphore.\n", thread_id);// 模拟线程执行一些任务sleep(2);printf("Thread %d is releasing the semaphore.\n", thread_id);sem_post(&semaphore);pthread_exit(NULL);
}int main() 
{pthread_t threads[NUM_THREADS];int thread_ids[NUM_THREADS];// 初始化信号量,初始值为1sem_init(&semaphore, 0, 1);// 创建多个线程for (int i = 0; i < NUM_THREADS; i++) {thread_ids[i] = i;pthread_create(&threads[i], NULL, thread_function, &thread_ids[i]);}// 等待所有线程结束for (int i = 0; i < NUM_THREADS; i++) {pthread_join(threads[i], NULL);}// 销毁信号量sem_destroy(&semaphore);return 0;
}

读写锁的使用

在编写多线程的时候,有一种情况是十分常见的。那就是,有些公共数据修改的机会比较少。相比较改写,它们读的机会反而高的多。通常而言,在读的过程中,往往伴随着查找的操作,中间耗时很长。给这种代码段加锁,会极大地降低我们程序的效率。 为了专门处理这种情况,有了读写锁。

使用

// 1. 设置读写优先级
// attr指向要设置属性的读写锁的指针
// pref 共有3种选择
//PTHREAD_RWLOCK_PREFER_READER_NP:优先选择读者。
//PTHREAD_RWLOCK_PREFER_WRITER_NP:优先选择写者。
//PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP:优先选择非递归写者。
int pthread_rwlockattr_setkind_np(pthread_rwlockattr_t *attr, 
int pref);//2. 初始化
//rwlock用于指定要初始化的读写锁对象。
//attr设置为nullptr
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);//3. 加锁和解锁/*pthread_rwlock_rdlock函数用于获取读写锁的读取锁定。
读取锁定允许多个线程同时持有锁,只要没有线程持有写入锁。
如果有线程持有写入锁,则其他线程尝试获取读取锁会被阻塞,直到写入锁被释放。*/
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);/*
pthread_rwlock_wrlock函数用于获取读写锁的写入锁定。
写入锁定是独占的,只能由一个线程持有。如果有线程持有读取锁或写入锁,
则其他线程尝试获取写入锁会被阻塞,直到所有读取锁和写入锁都被释放
*/
int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//用于释放读写锁
int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

实际使用:

#include <stdio.h>
#include <pthread.h>// 共享资源
int shared_data = 0;// 读写锁
pthread_rwlock_t rwlock;// 读线程函数
void* reader_function(void* arg) 
{int thread_id = *(int*)arg;while (1) {// 获取读锁pthread_rwlock_rdlock(&rwlock);// 读取共享资源printf("Reader %d reads shared data: %d\n", thread_id, shared_data);// 释放读锁pthread_rwlock_unlock(&rwlock);// 等待一段时间sleep(1);}pthread_exit(NULL);
}// 写线程函数
void* writer_function(void* arg) 
{int thread_id = *(int*)arg;while (1) {// 获取写锁pthread_rwlock_wrlock(&rwlock);// 修改共享资源shared_data++;printf("Writer %d updates shared data: %d\n", thread_id, shared_data);// 释放写锁pthread_rwlock_unlock(&rwlock);// 等待一段时间sleep(2);}pthread_exit(NULL);
}int main() 
{pthread_t readers[3];pthread_t writers[2];int reader_ids[3] = {1, 2, 3};int writer_ids[2] = {1, 2};// 初始化读写锁pthread_rwlock_init(&rwlock, NULL);// 创建读线程for (int i = 0; i < 3; i++) {pthread_create(&readers[i], NULL, reader_function, &reader_ids[i]);}// 创建写线程for (int i = 0; i < 2; i++) {pthread_create(&writers[i], NULL, writer_function, &writer_ids[i]);}// 等待读线程结束for (int i = 0; i < 3; i++) {pthread_join(readers[i], NULL);}// 等待写线程结束for (int i = 0; i < 2; i++) {pthread_join(writers[i], NULL);}// 销毁读写锁pthread_rwlock_destroy(&rwlock);return 0;
}

在这里插入图片描述

http://www.mnyf.cn/news/51816.html

相关文章:

  • go 是做网站的吗海外网站cdn加速
  • 网页制作一个网站八个页面咋做泰安百度公司代理商
  • 网站建站查询阿里云自助建站
  • 如何建网站教程视频百度指数官方
  • 网站策划书中应包括市场竞争对手的信息广州网站优化多少钱
  • 网站视频链接怎么做的石家庄百度推广优化排名
  • 中小企业网站制作费用是多少?cms系统
  • 阿里云服务器url做网站seo关键词找29火星软件
  • 兴平做网站一键优化清理手机
  • 女鞋网站建设策划方案域名站长工具
  • 做外贸网站赚钱吗申请自媒体平台注册
  • 什么是做网站创建网站的流程
  • 如何百度搜到自己网站seo培训班 有用吗
  • 男学网站开发网站收录情况查询
  • dedecms做网站怎么查看php开源建站系统
  • 做图片能去哪些网站线下推广方法及策略
  • 黄做网站手机如何制作网页
  • 北海教网站建设服务之家网站推广
  • 网站建设与维护是什么内容?企业网址
  • 做公司的网站有哪些东西吗南通seo
  • 做网站给源码吗seo思维
  • h5做商城网站软件优化
  • 义乌兼职网站建设磁力在线搜索引擎
  • android系统app开发店铺seo是什么意思
  • 网站后台 登录界面模板 远吗百度投诉中心24人工
  • 帮助中心网站模板合肥网站优化软件
  • 华人汇和科技园建设有限公司网站谷歌外链代发
  • 陶瓷企业 瓷砖地板公司网站建设宝鸡网站seo
  • 网站banner怎么做的wap网站html5
  • 英文网站google推广营销型企业网站制作