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

秦皇岛找一家能建网站的公司谷歌seo 外贸建站

秦皇岛找一家能建网站的公司,谷歌seo 外贸建站,一个人怎样做网站,项目计划书目录目录 1、概念 2、线程的实现: 3、线程同步: 4、使用信号量: 5、使用信号量实现进程同步: 6、使用互斥锁 7、使用互斥锁实现线程同步 8、读写锁 9、使用读写锁 10、使用读写锁实现进程同步 1、概念 线程:进程…

目录

1、概念

2、线程的实现:

3、线程同步:

4、使用信号量:

5、使用信号量实现进程同步:

6、使用互斥锁

7、使用互斥锁实现线程同步

8、读写锁

9、使用读写锁

10、使用读写锁实现进程同步


1、概念

线程:进程内部的一条执行路径

进程:正在运行的程序

并发:在一段时间内两者交替运行

并行,在一段时间内,两者同时执行,必须有两个处理器

2、线程的实现:

用户级线程:模拟出来多条路径,在内核眼里依旧是一条路径,他的出现不需要操作系统参与,可以创建特别多的线程,开销小但是无法使用多个处理器(只能并发)

(linux)内核级线程:通过内核提供的接口去创建线程,创建开销大,但是可以实现并行(使用多个处理器)

组合:可以利用多处理器的资源,在用户空间可以创建更多线程,对于后期线程的创建来说,开销小

在Linux系统上,没有线程的概念,每一个线程都是当作进程来实现的,是一个和其他进程共享某些资源的进程,都拥有属于自己的内存控制块

3、线程同步:

信号量     互斥锁     条件变量      读写锁

对程序的执行进行控制,保证程序的正确性,多个线程按照一定顺序执行。

没有进行同步线程的程序
int g_count=1;
void* fun(void *arv)
{
for(int i=0;i<1000;i++)
{
printf("g_count=%d",g_count++);
}
}
int main()
{
pthread_t id[5];
for(int i=0;i<5;i++)
{
pthread_creat(&id[i],NULL,fun,NULL);
}
for(int i=0;i<5;i++)
{
pthread_join(id[i],NULL);//等待其他线程结束
}
exit(0);
}
对于最终结果,因为在运行过程中可能出现在同一时间,两个线程对同一个数据进行操作的情况,所以结果可能为5000,也可能小于5000

4、使用信号量:

头文件#include<semphore.h>

初始化信号量:sem_t sem;

执行p操作:sem_wait(&sem);

执行v操作:sem_post(&sem);

销毁:sem_destroy(&sem);

5、使用信号量实现进程同步:

使用了信号量进行线程同步
#include<semaphore.h>
int g_count=1;
sem_t sem;
void* fun(void *arv)
{
for(int i=0;i<1000;i++)
{
sem_wait(&sem);//p操作
printf("g_count=%d",g_count++);
sem_post(&sem);//v操作
}
}
int main()
{
sem_init(&sem,0,1);//sem=1
//信号量初始化pthread_t id[5];
for(int i=0;i<5;i++)
{
pthread_creat(&id[i],NULL,fun,NULL);
}
for(int i=0;i<5;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_count=1;pthread_mutex_t mutex;
//定义互斥锁
void* fun(void *arv)
{
for(int i=0;i<1000;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 i=0;i<5;i++)
{
pthread_creat(&id[i],NULL,fun,NULL);
}
for(int i=0;i<5;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 i=0;i<10;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 i=0li<10;i++)
{
pthread_rwlock_rdlock(&lock);
printf("r1 star");
sleep(1);
printf("r1 end");
pthread_rwlock_rdlock(&lock);
}
}void*pthread_fun2(void*arg)
{
for(int i=0;i<5;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);
}

http://www.hkea.cn/news/969939/

相关文章:

  • 网站建设好做吗乐事薯片软文推广
  • wordpress 年月归档如何优化培训体系
  • 威海高区建设局网站长春做网络优化的公司
  • 安平做网站百度一下首页百度一下知道
  • 苏州建设网站市政中标项目如何做推广引流赚钱
  • 17网站一起做网店怎么下单来宾网站seo
  • 建设商务网站的目的天津seo网站排名优化公司
  • 阿里巴巴网站导航栏怎么做口碑营销策划方案
  • 线上做交互的网站百度app下载
  • 做暖暖欧美网站挖掘爱站网
  • 网站 风格百度推广公司
  • 林州网站建设公司站长工具关键词排名怎么查
  • 想给公司做个网站微信seo是什么意思
  • 网站做管制户外刀具营销推广方案模板
  • 淘宝客网站免费做seo网站关键词优化机构
  • 企业做网站建设的好处seo网站关键词优化
  • 一般网站用什么做的最新新闻国内大事件
  • 做线上网站需要钱吗互联网营销推广
  • 找个美工做淘宝网站需要多少钱南昌seo方案
  • 网站用户登录流程图外贸高端网站设计公司
  • 做搜狗手机网站优化软代写
  • wordpress页面背景颜色win7优化设置
  • 做分类信息网站代码百度搜索推广优化师工作内容
  • 南京网站开发公司关键词推广
  • 合水口网站建设百度指数明星人气榜
  • 上传网站图片处理推广软件免费
  • 做网站怎么写代码下载百度软件
  • 县城做网站网站搭建关键词排名
  • b2b多平台一键发布seo需要掌握哪些技术
  • 网站建设推广合同网络广告联盟