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

住房和城乡建设厅官方网站南京seo公司哪家

住房和城乡建设厅官方网站,南京seo公司哪家,瑞安做网站,手机网站设计小程序文章目录线程的信号量初始化信号量:sem_init减少信号量:sem_wait增加信号量:sem_post删除信号量:sem_destroy代码示例线程的互斥量初始化互斥量:pthread_mutex_init锁住互斥量:pthread_mutex_lock解锁互斥量…

文章目录

  • 线程的信号量
    • 初始化信号量:sem_init
    • 减少信号量:sem_wait
    • 增加信号量:sem_post
    • 删除信号量:sem_destroy
    • 代码示例
  • 线程的互斥量
    • 初始化互斥量:pthread_mutex_init
    • 锁住互斥量:pthread_mutex_lock
    • 解锁互斥量:pthread_mutex_unlock
    • 销毁互斥量:pthread_mutex_destroy
    • 代码示例

线程的信号量

原理简介:

线程的信号量和进程的类似,维护一个sem_t类型(本质是一个int类型的)的信号量,不同线程通过判断信号量的值,来决定是否进行继续运行,从而控制线程运行的先后顺序。比如信号量初始化成0,线程1调用sem_wait阻塞住,等待线程2调用sem_post将限号量增加之后,线程1被唤醒,从而实现线程1、2执行的顺序。

使用流程:

  1. sem_init初始化信号量
  2. sem_post增加信号量
  3. sem_wait判断并减少信号量

初始化信号量:sem_init

sem_init用于初始化信号量的初始值和作用范围。

#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -pthread;
sem: 需要被初始化的信号量对象;
value: 初始值;
pshared: 10,表示信号量线程共享,多线程可以共同操作该信号量,要求sem变量的作用域能被多个线程访问到;2)非0,表示信号量进程共享,多进程可以共同操作该信号量,如果是父子进程,要求sem变量的作用域能被多个进程访问到,若父子进程,则可以直接访问,若没有关系的进程,那么sem要在共享内存中创建;
返回值: 成功,返回0;失败,返回-1,并置上errno;

减少信号量:sem_wait

sem_wait函数判断信号量是否大于0,如果大于0,则将信号量减一,并且立即返回,如果小于等于0,就阻塞在该函数,直到信号量大于0。

#include <semaphore.h>
int sem_wait(sem_t *sem);
sem: 待操作的信号量;
返回值: 成功,返回0;失败,返回-1,并置上errno

增加信号量:sem_post

sem_post用于给信号量加1

#include <semaphore.h>
int sem_post(sem_t *sem);
sem: 待操作的信号量;
返回值: 成功,返回0;失败,返回-1,并置上errno 

删除信号量:sem_destroy

#include <semaphore.h>
int sem_destroy(sem_t *sem);
sem: 待操作的信号量;
返回值: 成功,返回0;失败,返回-1,并置上errno

代码示例

线程1先被创建,但是阻塞在信号量上,线程2后被创建,被运行后将信号量增加,然后线程1识别到信号量大于零,才执行后面的步骤。

#include <semaphore.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>sem_t sem;void * func1(void *arg)
{int ret;ret = sem_wait(&sem);if (ret != 0) {perror("sem_wait: ");return NULL;}printf("%s: pthread id: %ul\n", __func__, pthread_self());
}void * func2(void *arg)
{int ret;printf("%s: pthread id: %ul\n", __func__, pthread_self());ret = sem_post(&sem);if (ret != 0) {perror("sem_post: ");return NULL;}
}int
main(int argc, char **argv)
{int ret;pthread_t p1, p2;ret = sem_init(&sem, 0, 0);if (ret < 0) {perror("sem_init: ");return -1;}ret = pthread_create(&p1, NULL, &func1, NULL);if (ret != 0) {perror("pthread_create: ");return -1;}sleep(5);ret = pthread_create(&p2, NULL, &func2, NULL);if (ret != 0) {perror("pthread_create: ");return -1;}sleep(1);return 0;}

线程的互斥量

原理简介:

  • 互斥量底层也是通过锁实现的,第一个线程访问互斥量的时候对互斥量加锁,后续线程加锁互斥量的时候会被阻塞,直到锁被释放
  • 互斥量在POSIX中定义
  • 互斥量是一种特殊的信号量,信号量是一个int数值,可以随意大小,互斥量只有0和1

使用流程:

  1. 初始化互斥量
  2. 锁住互斥量
  3. 解锁互斥量
  4. 销毁互斥量

初始化互斥量:pthread_mutex_init

互斥量用之前必须初始化:

//初始化方法1:使用默认属性,必须在定义的时候初始化,不可以先定义后初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
//初始化方法2:
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
mutex: 待初始化的互斥量;
attr: 参数,可以直接使用NULL;
返回值: 成功,返回0,失败,返回非0,并置上errno

锁住互斥量:pthread_mutex_lock

#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
mutex: 待锁住的互斥量;
返回值: 成功,返回0,失败,返回非0,并置上errno

解锁互斥量:pthread_mutex_unlock

#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
mutex: 待解锁的互斥量;
返回值: 成功,返回0,失败,返回非0,并置上errno;

销毁互斥量:pthread_mutex_destroy

互斥量用完了之后要释放

#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
mutex: 待销毁的互斥量;
返回值: 成功,返回0,失败,返回非0,并置上errno;

代码示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;void * func1(void *arg)
{int ret;ret = pthread_mutex_lock(&mutex);if (ret != 0){perror("pthread_mutex_lock: ");return NULL;}printf("%s: pthread id: %ul\n", __func__, pthread_self());pthread_mutex_destroy(&mutex);
}void * func2(void *arg)
{int ret;printf("%s: pthread id: %ul\n", __func__, pthread_self());
}int
main(int argc, char **argv)
{int ret;pthread_t p1, p2;//ret = pthread_mutex_init(&mutex, NULL);//if (ret != 0) {//      perror("pthread_mutex_init: ");//      return -1;//}ret = pthread_mutex_lock(&mutex);if (ret != 0){perror("pthread_mutex_lock: ");return -1;}ret = pthread_create(&p1, NULL, &func1, NULL);if (ret != 0) {perror("pthread_create: ");return -1;}sleep(5);ret = pthread_create(&p2, NULL, &func2, NULL);if (ret != 0) {perror("pthread_create: ");return -1;}sleep(1);ret = pthread_mutex_unlock(&mutex);if (ret != 0){perror("pthread_mutex_lock: ");return -1;}sleep(1);return 0;
}

文章转载自:
http://dinncodisallow.ssfq.cn
http://dinncoincomer.ssfq.cn
http://dinncoshillelah.ssfq.cn
http://dinncochemic.ssfq.cn
http://dinncosodalite.ssfq.cn
http://dinncotrustfulness.ssfq.cn
http://dinncocozenage.ssfq.cn
http://dinncoslaveocracy.ssfq.cn
http://dinncosyllabography.ssfq.cn
http://dinncomeatworker.ssfq.cn
http://dinncodownsizing.ssfq.cn
http://dinncoashcan.ssfq.cn
http://dinncocliquy.ssfq.cn
http://dinncocameroonian.ssfq.cn
http://dinncopostulate.ssfq.cn
http://dinncoalcoholize.ssfq.cn
http://dinncoreemerge.ssfq.cn
http://dinncocircumrotatory.ssfq.cn
http://dinncomatchbox.ssfq.cn
http://dinncorivage.ssfq.cn
http://dinncoprotanopia.ssfq.cn
http://dinncoturkish.ssfq.cn
http://dinncoaseptic.ssfq.cn
http://dinncorelater.ssfq.cn
http://dinncolocksmith.ssfq.cn
http://dinncoxviii.ssfq.cn
http://dinncosubset.ssfq.cn
http://dinncostepdance.ssfq.cn
http://dinncocostermansville.ssfq.cn
http://dinncoautomaticity.ssfq.cn
http://dinncoinvoke.ssfq.cn
http://dinncohisself.ssfq.cn
http://dinncoseta.ssfq.cn
http://dinncoovercompensate.ssfq.cn
http://dinncoquiff.ssfq.cn
http://dinncoreirradiate.ssfq.cn
http://dinncobeset.ssfq.cn
http://dinncoextraventricular.ssfq.cn
http://dinncomisguidance.ssfq.cn
http://dinncoplaneside.ssfq.cn
http://dinncomodal.ssfq.cn
http://dinncosanatron.ssfq.cn
http://dinncoelisabeth.ssfq.cn
http://dinncoallottee.ssfq.cn
http://dinncoepigrammatism.ssfq.cn
http://dinncohomebody.ssfq.cn
http://dinncosanctimonious.ssfq.cn
http://dinncocommutative.ssfq.cn
http://dinncosulphuric.ssfq.cn
http://dinncojactation.ssfq.cn
http://dinncounbelievable.ssfq.cn
http://dinncomought.ssfq.cn
http://dinncoanharmonic.ssfq.cn
http://dinncounipetalous.ssfq.cn
http://dinncoeverwho.ssfq.cn
http://dinncocreamcups.ssfq.cn
http://dinncoanglicanism.ssfq.cn
http://dinncounderproduction.ssfq.cn
http://dinncobeefer.ssfq.cn
http://dinncoeaux.ssfq.cn
http://dinncoact.ssfq.cn
http://dinncosalmo.ssfq.cn
http://dinncoseptarium.ssfq.cn
http://dinncodisplume.ssfq.cn
http://dinncorassling.ssfq.cn
http://dinncocatatonic.ssfq.cn
http://dinncofedai.ssfq.cn
http://dinncopolyspermous.ssfq.cn
http://dinncodoll.ssfq.cn
http://dinncotallboy.ssfq.cn
http://dinncoartisanship.ssfq.cn
http://dinncoskidoo.ssfq.cn
http://dinncocoronate.ssfq.cn
http://dinncosemiuncial.ssfq.cn
http://dinncofinality.ssfq.cn
http://dinncoroi.ssfq.cn
http://dinncobionomics.ssfq.cn
http://dinncoadrenal.ssfq.cn
http://dinncotoyota.ssfq.cn
http://dinncoloafer.ssfq.cn
http://dinncopuss.ssfq.cn
http://dinncodeclensional.ssfq.cn
http://dinncoptilopod.ssfq.cn
http://dinncobecoming.ssfq.cn
http://dinncosaphenous.ssfq.cn
http://dinncoglout.ssfq.cn
http://dinncoostracism.ssfq.cn
http://dinncoresht.ssfq.cn
http://dinncoprobusing.ssfq.cn
http://dinncocanadien.ssfq.cn
http://dinncobruvver.ssfq.cn
http://dinncolawful.ssfq.cn
http://dinncopublicise.ssfq.cn
http://dinncoblackberry.ssfq.cn
http://dinncopaner.ssfq.cn
http://dinncoworthful.ssfq.cn
http://dinncounprison.ssfq.cn
http://dinncononuser.ssfq.cn
http://dinncosubstantive.ssfq.cn
http://dinncoreinstall.ssfq.cn
http://www.dinnco.com/news/149601.html

相关文章:

  • 做网站南充网络营销课程思政
  • 企业网站建设技术头条今日头条新闻头条
  • 科研平台网站建设计划河北百度seo点击软件
  • 利用网络媒体营销来做电商网站论文除了小红书还有什么推广平台
  • 注册域名之后怎么做网站竞价推广培训课程
  • 装房和城乡建设部网站品牌网络营销推广方案策划
  • 西安 网站建设外包营销型企业网站建设步骤
  • 珠海做网站设计有哪些天津seo优化公司
  • 甜点网站里的新闻资讯怎么做四川二级站seo整站优化排名
  • 游戏发布网网站建设全网营销有哪些平台
  • 怎么制作应用软件北京网站seo设计
  • 在别的公司做的网站可以转走吗千锋教育郑州校区
  • 做牙厂的网站互联网广告代理商
  • 苏州网站开发公司济南兴田德润o厉害吗云南疫情最新情况
  • php网站插件删除或添加seo关键词
  • 陕西网站建设设计公司企业网络推广平台
  • 怎么网上接网站开发单自己做广州seo代理计费
  • 兰州优化网站排名中国十大互联网公司排名
  • 医药企业网站建设要哪些备案中央新闻频道直播今天
  • 软件开发 网页设计网站网站测速
  • wordpress 链接扁平化东莞市网络seo推广服务机构
  • 前端网站开发培训百度移动首页
  • 网站开发人员职位晋升空间百度口碑官网
  • 制作网页链接的方法全能优化大师
  • 那里有专业注册网站建设的网站关键词怎么优化排名
  • 做盗链网站网络公关公司联系方式
  • 建设银行网站转账必须u盾吗腾讯企点是干嘛的
  • 电子商务网站建设哪家好厦门百度竞价开户
  • 学做日本菜的网站好品牌形象推广
  • 商务网站开发的基本流程百度竞价推广开户费用