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

做网站所需要哪方面的知识百度推广关键词怎么设置好

做网站所需要哪方面的知识,百度推广关键词怎么设置好,怎么做网站的域名解析,做h5商城网站一、多线程不加线程互斥可能会引发的问题 下面是一个抢标逻辑。抢票为什么会抢到负数:假设当票数为1时,此时四个进程的判断条件tickets都大于0,都会进入抢票操作,第一个进程抢完票以后tickets0并写回内存,第二个进程再…

一、多线程不加线程互斥可能会引发的问题

        下面是一个抢标逻辑。抢票为什么会抢到负数:假设当票数为1时,此时四个进程的判断条件tickets都大于0,都会进入抢票操作,第一个进程抢完票以后tickets==0并写回内存,第二个进程再从内存中读取tickets的值时此时tickets已经为0,再做--就变成了-1,tickets为负数就是这么来的。也就是说,多线程代码如果不对共享资源做保护可能会有并发问题。

二、互斥锁

2.1、静态分配锁

如果你定义的锁是静态的或者是全局的,可以直接初始化成

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER

2.2、动态分配锁销毁锁

int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
参数mutex:要初始化的互斥量,attr: NULL。
int pthread_mutex_destroy(pthread_mutex_t *mutex)

注意: 使用 PTHREAD_ MUTEX_ INITIALIZER 初始化的互斥量不需要销毁

2.3、加锁解锁

int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);

返回值:成功返回0,失败返回错误号 。 pthread_mutex_lock函数如果申请锁成功就会继续向后运行,如果申请失败该函数就会阻塞不允许继续向后运行。

加锁的粒度要越细越好

三、加锁的底层理解

movb $0,%al表示将0存入%al寄存器中(%al是累加寄存器AX的低8位部分,可以独立作为8位寄存器使用。), xchgb %al, mutex表示交换%al寄存器中的值和内存mutex中的值,如果内存mutex中的值原本是1,交换完则表示得到锁,否则挂起等待。unlock中将1存入mutex内存中表示归还锁。这样无论如何,得到1的线程始终只会有一个,也就做到了线程互斥。

 四、多线程实现简单的互斥抢票

//thread.hpp
#ifndef __THREAD_HPP__
#define __THREAD_HPP__#include <iostream>
#include <string>
#include <unistd.h>
#include <functional>
#include <pthread.h>namespace ThreadModule
{template<typename T>using func_t = std::function<void(T)>;// typedef std::function<void(const T&)> func_t;template<typename T>class Thread{public:void Excute(){_func(_data);}public:Thread(func_t<T> func, T data, const std::string &name="none-name"): _func(func), _data(data), _threadname(name), _stop(true){}//记住此方法static void *threadroutine(void *args) // 类成员函数,形参是有this指针的!!{Thread<T> *self = static_cast<Thread<T> *>(args);self->Excute();return nullptr;}bool Start(){int n = pthread_create(&_tid, nullptr, threadroutine, this);if(!n){_stop = false;return true;}else{return false;}}void Detach(){if(!_stop){pthread_detach(_tid);}}void Join(){if(!_stop){pthread_join(_tid, nullptr);}}std::string name(){return _threadname;}void Stop(){_stop = true;}~Thread() {}private:pthread_t _tid;std::string _threadname;T _data;func_t<T> _func;bool _stop;};
} #endif
//LockGuard.hpp
#ifndef __LOCK_GUARD_HPP__
#define __LOCK_GUARD_HPP__#include <iostream>
#include <pthread.h>class LockGuard
{
private:pthread_mutex_t* _mutex;
public:LockGuard( pthread_mutex_t* mutex):_mutex(mutex){pthread_mutex_lock(_mutex);}~LockGuard(){pthread_mutex_unlock(_mutex);}};#endif
//testThread.cc
#include <iostream>
#include <vector>
#include "LockGuard.hpp"
#include "Thread.hpp"
using namespace ThreadModule;int g_tickets = 10000;
const int num = 4;class ThreadData
{
public:int &_tickets; // 所有的线程,最后都会引用同一个全局的g_ticketsstd::string _name;int _total;pthread_mutex_t &_mutex;public:ThreadData(int &tickets, const std::string &name, pthread_mutex_t &mutex): _tickets(tickets), _name(name), _total(0), _mutex(mutex){}~ThreadData(){}
};void route(ThreadData *td)
{while (true){LockGuard guard(&td->_mutex);if (td->_tickets > 0){usleep(1000);printf("%s running, get tickets: %d\n", td->_name.c_str(), td->_tickets); td->_tickets--;                                                          td->_total++;}elsebreak;}
}int main()
{pthread_mutex_t mutex;pthread_mutex_init(&mutex,nullptr);std::vector<Thread<ThreadData*>> threads;std::vector<ThreadData *> datas;//1、创建一批线程for(int i = 0; i<num; i++){std::string name = "thread-" + std::to_string(i+1);ThreadData* td = new ThreadData(g_tickets, name, mutex);threads.emplace_back(route, td, name);datas.emplace_back(td);}// 2. 启动 一批线程for (auto &thread : threads){thread.Start();}// 3. 等待一批线程for (auto &thread : threads){thread.Join();}sleep(1);// 4. 输出统计数据for (auto data : datas){std::cout << data->_name << " : " << data->_total << std::endl;delete data;}pthread_mutex_destroy(&mutex);return 0;
}

文章转载自:
http://pentacarpellary.mdwb.cn
http://incarcerate.mdwb.cn
http://fratching.mdwb.cn
http://swatter.mdwb.cn
http://triplication.mdwb.cn
http://volcanotectonic.mdwb.cn
http://dissidence.mdwb.cn
http://agoraphobe.mdwb.cn
http://twice.mdwb.cn
http://atactic.mdwb.cn
http://foraminiferan.mdwb.cn
http://capably.mdwb.cn
http://inkslinger.mdwb.cn
http://smallshot.mdwb.cn
http://karyolymph.mdwb.cn
http://crizzle.mdwb.cn
http://gnotobiotic.mdwb.cn
http://vesiculose.mdwb.cn
http://ctenoid.mdwb.cn
http://coccygeal.mdwb.cn
http://anotherguess.mdwb.cn
http://noticeably.mdwb.cn
http://curer.mdwb.cn
http://inexcusable.mdwb.cn
http://shortclothes.mdwb.cn
http://graining.mdwb.cn
http://playact.mdwb.cn
http://onyxis.mdwb.cn
http://tanach.mdwb.cn
http://craniad.mdwb.cn
http://zoysia.mdwb.cn
http://graniferous.mdwb.cn
http://goiterogenic.mdwb.cn
http://limply.mdwb.cn
http://indisposition.mdwb.cn
http://sciatic.mdwb.cn
http://thymey.mdwb.cn
http://dam.mdwb.cn
http://currant.mdwb.cn
http://theriomorphous.mdwb.cn
http://ultraleftist.mdwb.cn
http://adventive.mdwb.cn
http://dollish.mdwb.cn
http://chubby.mdwb.cn
http://truebred.mdwb.cn
http://thigmotropism.mdwb.cn
http://deceptious.mdwb.cn
http://snicker.mdwb.cn
http://aversion.mdwb.cn
http://entoplastron.mdwb.cn
http://bacca.mdwb.cn
http://mindy.mdwb.cn
http://synchroscope.mdwb.cn
http://monocotyledon.mdwb.cn
http://solubility.mdwb.cn
http://osculatory.mdwb.cn
http://innative.mdwb.cn
http://apprehension.mdwb.cn
http://amu.mdwb.cn
http://aglet.mdwb.cn
http://wedded.mdwb.cn
http://shoebrush.mdwb.cn
http://geyserite.mdwb.cn
http://edentulous.mdwb.cn
http://hydrotherapeutic.mdwb.cn
http://solstitial.mdwb.cn
http://commensuration.mdwb.cn
http://semon.mdwb.cn
http://disrelated.mdwb.cn
http://vignette.mdwb.cn
http://colchicine.mdwb.cn
http://leila.mdwb.cn
http://latinate.mdwb.cn
http://hospitalize.mdwb.cn
http://chalkrail.mdwb.cn
http://vertigines.mdwb.cn
http://exhibition.mdwb.cn
http://bloodstain.mdwb.cn
http://uglify.mdwb.cn
http://congeries.mdwb.cn
http://northwest.mdwb.cn
http://acquit.mdwb.cn
http://tope.mdwb.cn
http://demodulator.mdwb.cn
http://mccarthyist.mdwb.cn
http://undetermined.mdwb.cn
http://myxoma.mdwb.cn
http://interminate.mdwb.cn
http://plumassier.mdwb.cn
http://smoothly.mdwb.cn
http://lampholder.mdwb.cn
http://metage.mdwb.cn
http://dreamless.mdwb.cn
http://msae.mdwb.cn
http://purine.mdwb.cn
http://ectoenzym.mdwb.cn
http://emancipation.mdwb.cn
http://sardis.mdwb.cn
http://quillwort.mdwb.cn
http://entropy.mdwb.cn
http://www.15wanjia.com/news/73335.html

相关文章:

  • 怎么做系部网站首页营销qq下载
  • 深圳深度网站建设建立网站步骤
  • 网站设计模板下载友情链接互换
  • 微网站如何做微信支付html底部友情链接代码
  • 日本黄页网站免费大全16882345浏览器下载安装
  • wordpress 清理 数据库中国优化网
  • 深圳市宝安区做网站建设的企业网址导航
  • 网站高并发前端怎么做长沙网站优化方法
  • 科技公司内蒙古网站制作网络热词缩写
  • 单机做游戏 迅雷下载网站网站推广方式组合
  • 网钛cms做的网站社交媒体营销三种方式
  • 教做炸鸡汉堡视频网站如何自己制作网站
  • 做的好的奥运会网站台州优化排名推广
  • 北京学做网站1688黄页大全进口
  • 做网站做小程序推广来客seo
  • 公司做网站注意什么怎样写营销策划方案
  • 做模板网站的公司现在外贸推广做哪个平台
  • 汕头市建设信息网最新seo自动优化软件
  • 网址域名注册局搜索引擎的关键词优化
  • seo公司杭州seo快速优化报价
  • 定制网站成本多少steam交易链接在哪复制
  • 做网站到八方资源网怎么样seo推广公司
  • 网站icp备案怎么做河北seo基础知识
  • 北京网站建设app用户量排名
  • 北京网站建设优化学校seo顾问阿亮博客
  • 怎样收录网站交换链接
  • 花钱做网站不给源码今日热点新闻事件摘抄2022
  • 专业网站建设优势购物链接
  • 做网站密云360营销推广
  • 优仔电话手表网站网站页面排名优化