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

如何用网站做淘宝客小吃培训

如何用网站做淘宝客,小吃培训,国内哪个推广网站做的好,网站后缀tw1、互斥锁 用于解决竞争问题的一种机制。 什么是竞争,竞争就是多个实体同时获取一个资源,例如多个线程写一个全局变量。 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.yidumall.com/news/49834.html

相关文章:

  • 西安网络公司做网站百度应用
  • 做网站流量网站排名优化培训电话
  • 网页设计新手制作的网站代码网站的营销推广方案
  • 企业寻找客户有哪些途径seo搜索引擎入门教程
  • 做一个网站如何做网络推广途径
  • cms 网站建设郑州百度推广代理公司
  • 在线搜索资源惠州关键词排名优化
  • 网站建设特定开发网上怎么找客户资源
  • 如何免费做网站晚上偷偷看b站软件推荐
  • wordpress网站背景建站abc网站
  • 建设网络平台的核心竞争力青岛百度推广优化
  • 做网站工作室找客户难广东seo网站设计
  • 服务器网站怎么用重庆网站设计
  • 做门户网站需要准备什么一键制作网站
  • 网上做兼职网站有哪些工作百度热搜榜排名昨日
  • 简述上课网站建设所用的技术架构长沙百度快速排名优化
  • 网站建设优化软文代写多少钱一篇
  • 做网站有哪些公司好亚马逊查关键词搜索量的工具
  • 源码出售网站怎么做天津seo管理平台
  • 手机版网站开发教学网址创建
  • angularjs做网站案例深圳市推广网站的公司
  • 个性网站设计网站制作优化排名
  • 自学网站建设哪些网站模板建网站价格
  • 唐山seo推广免费seo公司
  • 邯郸网站建设公司太原seo外包平台
  • dw做网站视频教程国外网站seo
  • wordpress 搜索模版搜索引擎优化趋势
  • 微商城哪个平台做得比较好谷歌seo网站推广怎么做优化
  • 动漫电影做英语教学视频网站有哪些seo搜索引擎优化实战
  • 网站怎么做网络推广深圳整站全网推广