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

柳州网站建设找哪家网站做动态图片不显示

柳州网站建设找哪家,网站做动态图片不显示,wordpress模板主题介绍,网站建设后台管理怎么管理1、互斥锁 用于解决竞争问题的一种机制。 什么是竞争,竞争就是多个实体同时获取一个资源,例如多个线程写一个全局变量。 2、Linux如何使用互斥锁 以pthread为例,锁的创建和使用如下: /* 创建锁 */ pthread_mutex_t lock PTHR…

1、互斥锁

用于解决竞争问题的一种机制。

什么是竞争,竞争就是多个实体同时获取一个资源,例如多个线程写一个全局变量。

2、Linux如何使用互斥锁

以pthread为例,锁的创建和使用如下:

/* 创建锁 */
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;/* 加锁*/
pthread_mutex_lock(&lock);/* 解锁 */
pthread_mutex_unlock(&lock);

3、多写者问题


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <pthread.h>/* global variable */
int gValue = 0;pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void *threadFuncReader(void *arg)
{while(1){gValue = 1;if(1 != gValue){printf("\nerror");}// printf("\nthis is [1], read value is [%d]", gValue);usleep(1);}}void *threadFuncWriter(void *arg)
{while(1){gValue = 2;if(2 !=gValue ){printf("\nerror");}else{// printf("\nthis is [2], read value is [%d]", gValue);          }usleep(1);}}void *threadFuncWriter1(void *arg)
{while(1){gValue = 3;if(3 != gValue ){printf("\nerror");}else{//printf("\nthis is [3], read value is [%d]", gValue);          }usleep(1);}}void *threadFuncWriter2(void *arg)
{while(1){gValue = 4;if(4 != gValue ){printf("\nerror");}else{printf("\nthis is [4], read value is [%d]", gValue);          }sleep(1);}}int main(int argc, int **argv)
{/* shared resources */pthread_t tid_reader;pthread_t tid_writer;pthread_t tid_writer1;pthread_t tid_writer2;pthread_create(&tid_reader, NULL, threadFuncReader, NULL);pthread_create(&tid_writer, NULL, threadFuncWriter, NULL);pthread_create(&tid_writer1, NULL, threadFuncWriter1, NULL);pthread_create(&tid_writer2, NULL, threadFuncWriter2, NULL);pthread_join(tid_reader, NULL);pthread_join(tid_writer, NULL);pthread_join(tid_writer1, NULL);pthread_join(tid_writer2, NULL);printf("\n hello world in Linux.");return 0;
}

上述代码由于没有锁,运行结果如下:

this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
error
this is [4], read value is [4]

加锁后的代码:


#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdlib.h>
#include <pthread.h>/* global variable */
int gValue = 0;pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;void *threadFuncReader(void *arg)
{while(1){pthread_mutex_lock(&lock);gValue = 1;if(1 != gValue){printf("\nerror");}// printf("\nthis is [1], read value is [%d]", gValue);pthread_mutex_unlock(&lock);usleep(1);}}void *threadFuncWriter(void *arg)
{while(1){pthread_mutex_lock(&lock);gValue = 2;if(2 !=gValue ){printf("\nerror");}else{// printf("\nthis is [2], read value is [%d]", gValue);          }pthread_mutex_unlock(&lock);usleep(1);}}void *threadFuncWriter1(void *arg)
{while(1){pthread_mutex_lock(&lock);gValue = 3;if(3 != gValue ){printf("\nerror");}else{//printf("\nthis is [3], read value is [%d]", gValue);          }pthread_mutex_unlock(&lock);usleep(1);}}void *threadFuncWriter2(void *arg)
{while(1){pthread_mutex_lock(&lock);gValue = 4;if(4 != gValue ){printf("\nerror");}else{printf("\nthis is [4], read value is [%d]", gValue);          }pthread_mutex_unlock(&lock);sleep(1);}}int main(int argc, int **argv)
{/* shared resources */pthread_t tid_reader;pthread_t tid_writer;pthread_t tid_writer1;pthread_t tid_writer2;pthread_create(&tid_reader, NULL, threadFuncReader, NULL);pthread_create(&tid_writer, NULL, threadFuncWriter, NULL);pthread_create(&tid_writer1, NULL, threadFuncWriter1, NULL);pthread_create(&tid_writer2, NULL, threadFuncWriter2, NULL);pthread_join(tid_reader, NULL);pthread_join(tid_writer, NULL);pthread_join(tid_writer1, NULL);pthread_join(tid_writer2, NULL);printf("\n hello world in Linux.");return 0;
}

运行结果:

this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]
this is [4], read value is [4]

http://www.yayakq.cn/news/732282/

相关文章:

  • 网站开发后台数据怎么来创建一个网站的费用
  • 怎么自己制作一个网站广告联盟下载
  • 重庆网上房地产查询宁波seo推广方式排名
  • 南通 网站建设搞笑视频网站建设策划书
  • 买CAD设计图做的网站免费做的网站怎么设置域名解析
  • 用ps做网站的首页海口网站建设费用
  • 网站建设接单源码网站开发还是做数据库开发
  • 作网站公司天津建设工程信息网招聘
  • dede网站不能够生成玩具网站 下载
  • 找工作的网站做胃肠科网站
  • 棠下手机网站建设有名的网站制作公司
  • 做微信网站公司名称c 做网站怎么连接到别的网页
  • 爱用建站下载织梦响应式茶叶网站模板
  • 网站增加权重吗成都企业建网站
  • 小网站 收入找人做的网站第二年多钱
  • 柳州企业网站开发平台佛山网站设计外包
  • 买汽车最好的网站建设免费手机网页制作
  • 免费网站建设魔坊html5 app开发工具
  • 资深做网站公司培训课程ui设计
  • 网站开发企业排名北京青鸟培训机构哪家是正规的
  • 网站开发全程实例课本代码一件代发货源app
  • 专业做中文网站百度竞价推广怎么样
  • 贵州网站开发哪家便宜网站生成app要多少钱
  • 网站搭建申请ps做网页
  • 邯郸网站制作基本流程杭州网站建设的公司
  • jsp网站开发书籍推荐提供商城网站制作
  • 做网站需要学什么专业ccd设计公司很厉害吗
  • 建设网站改版wordpress全站cdn教程
  • 淘宝导购网站源码wordpress 表前缀
  • 网站开发流程中网站制作包括怎样做后端数据传输前端的网站