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

做电子商务网站建设工资多少钱网站开发完了备案

做电子商务网站建设工资多少钱,网站开发完了备案,佛山+客户端官网,做ppt高手 一定要常去这八个网站目录 一、问题引入 二、实现线程同步的方案——条件变量 1、常用接口#xff1a; 2、使用示例 一、问题引入 我们再次看看上次讲到的多线程抢票的代码#xff1a;这次我们让一个线程抢完票之后不去做任何事。 #include iostream #include unistd.h #inc…目录 一、问题引入 二、实现线程同步的方案——条件变量 1、常用接口 2、使用示例 一、问题引入 我们再次看看上次讲到的多线程抢票的代码这次我们让一个线程抢完票之后不去做任何事。 #include iostream #include unistd.h #include cstring #include time.h #include pthread.husing namespace std; #define THREAD_NUM 5class threaddata { public:threaddata(const string s, pthread_mutex_t *m): name(s), mtx(m){}public:string name;pthread_mutex_t *mtx; };int ticket 100;void *getticket(void *arg) {threaddata *td (threaddata *)arg;while (true){pthread_mutex_lock(td-mtx); if (ticket 0) {usleep(rand() % 10000);cout td-name : ticket endl;ticket--;pthread_mutex_unlock(td-mtx); }else{pthread_mutex_unlock(td-mtx); break;}}delete td;return nullptr; }int main() {pthread_mutex_t mtx;pthread_mutex_init(mtx, nullptr);pthread_t t[THREAD_NUM];for (int i 0; i THREAD_NUM; i){string name thread ;name to_string(i 1);threaddata *td new threaddata(name, mtx);pthread_create(t i, nullptr, getticket, (void *)td);}for (int i 0; i THREAD_NUM; i)pthread_join(t[i], nullptr);pthread_mutex_destroy(mtx);return 0; } 运行结果 我们这就发现了一个问题对于抢票系统我们看到的是只有一个线程5在一直连续抢票没有其他的线程。这很不合理。 这是因为如果个别线程的竞争力特别强每次都能够申请到锁但申请到锁之后什么也不做所以在我们看来这个线程就一直在申请锁和释放锁那么它就可以一直抢票。这就可能导致其他线程长时间竞争不到锁造成了其他线程的饥饿问题无法抢票。虽然你是拿到锁后再去访问临界资源并且最后还释放了锁由于竞争能力太强可以一直拿到锁这没有错但这不合理。 为了解决这个问题我们增加一个限制当一个线程释放锁后这个线程不能立马再次申请锁该线程必须排到这个锁的资源等待队列的最后。这样我们就有了线程同步我们在保证数据安全的情况下让这些线程按照一定的顺序进行临界资源的访问这就是线程同步。 竞态条件因为时序问题而导致程序异常我们称为竞态条件。 二、实现线程同步的方案——条件变量 当一个线程互斥地访问某个变量时它可能发现在其他线程改变状态之前它什么也做不了 例如一个线程访问队列时发现队列为空它只能等待直到其他线程将一个节点添加到队列中。这种情况就需要用到条件变量。 1、常用接口 1、条件变量的定义和初始化 ​ NAMEpthread_cond_destroy, pthread_cond_init - destroy and initialize condition variablesSYNOPSIS#include pthread.h//销毁int pthread_cond_destroy(pthread_cond_t *cond);//初始化int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);//全局和静态变量初始化pthread_cond_t cond PTHREAD_COND_INITIALIZER; 2、线程等待临界资源  pthread_cond_wait 功能一就是让线程在特定的条件变量下等待二就是让线程在等待时释放对应的互斥锁。当线程被唤醒时该函数会帮助我们线程获取锁。 #include pthread.h //特定时间阻塞等待 int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict abstime); //等待 int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex); 3、唤醒线程去访问临界资源 #include pthread.h // 唤醒所有等待的线程 int pthread_cond_broadcast(pthread_cond_t *cond);// 唤醒一个线程 int pthread_cond_signal(pthread_cond_t *cond); 注1、条件变量通常需要配合互斥锁一起使用。 2、条件变量的使用一个线程等待条件变量的条件成立而被挂起另一个线程使条件成立后唤醒等待的线程。 3、等待的时候往往是在临界区内等待的。加锁与解锁之间的区域进行等待 4、线程被唤醒是在之前进行等待的地方被唤醒。 2、使用示例 有了线程同步我们就可以改进我们之前的抢票系统的代码 #include iostream #include string #include time.h #include unistd.h #include pthread.husing namespace std; #define THREADNUM 3 typedef void *(*func)(void *argc);class threaddata { public:threaddata(pthread_mutex_t *mtx, pthread_cond_t *cond, const string name): mtx_(mtx), cond_(cond), name_(name){}public:pthread_mutex_t *mtx_;pthread_cond_t *cond_;string name_; };int ticket 100;void *getticket(void *arg) {threaddata *td (threaddata *)arg;while (true){pthread_mutex_lock(td-mtx_);pthread_cond_wait(td-cond_, td-mtx_); // 在加锁和解锁之间进行等待if (ticket 0){usleep(rand() % 10000);cout td-name_ : ticket endl;ticket--;pthread_mutex_unlock(td-mtx_);}else{pthread_mutex_unlock(td-mtx_);break;}}delete td;return nullptr; }void *fun1(void *arg) {threaddata *td (threaddata *)arg;while (true){getticket((void *)td);sleep(1);} }void *fun2(void *arg) {threaddata *td (threaddata *)arg;while (true){getticket((void *)td);sleep(1);} }void *fun3(void *arg) {threaddata *td (threaddata *)arg;while (true){getticket((void *)td);sleep(1);} }int main() {srand((unsigned long)time(nullptr) ^ getpid() ^ 433);pthread_mutex_t mtx;pthread_cond_t cond;pthread_mutex_init(mtx, nullptr);pthread_cond_init(cond, nullptr);pthread_t t[THREADNUM];func fun[THREADNUM] {fun1, fun2, fun3};for (int i 0; i THREADNUM; i){string name thread ;name to_string(i 1);threaddata *td new threaddata(mtx, cond, name);pthread_create(t i, nullptr, fun[i], (void *)td);}sleep(5);while (true){pthread_cond_signal(cond);sleep(1);}for (int i 0; i THREADNUM; i)pthread_join(t[i], nullptr);pthread_mutex_destroy(mtx);pthread_cond_destroy(cond);return 0; } 如果我们每次都想将在该条件变量下等待的所有线程进行唤醒可以将代码中的pthread_cond_signal函数改为pthread_cond_broadcast函数。  此时我们会发现唤醒这三个线程时具有明显的顺序性因为这些线程启动时默认都会在该条件变量下去等待而我们每次都唤醒的是在当前条件变量下等待的头部线程当该线程执行完代码后会继续排到等待队列的尾部进行等待。
http://www.hkea.cn/news/14460985/

相关文章:

  • 江西城市建设管理协会网站天津市招标投标信息网
  • 网站源码资源什么是网站推广
  • 新竹网站零食网站建设前的市场分析
  • 广东省建设工程安全协会网站wordpress大访问量
  • 定州网站建设安源网站建设
  • 哪里做网站最便宜国际最新军事新闻
  • 厦门网站建设外包科普网站建设方案书
  • 仿网站工具wordpress设置视频图片
  • 福州电商网站设计快手刷评论推广网站
  • wordpress建站模板下载wordpress 不显示主题
  • 网站建设的中期检查表网站怎么做身份验证
  • 使用阿里云建网站绵阳优化网站排名
  • 深圳市城乡住房和建设局网站首页门户网站开发模板
  • 做网站源码要给客户嘛上海搬家公司收费
  • 怎样给网站做外链长沙速马科技
  • 宿迁建设局质安站网站免费制作海报
  • 天津高端模板建站cms开发教程
  • 免费建站个人网站北京食药局网站年检怎么做
  • 高端的网站优化公司鹤壁建设网站推广公司电话
  • 做房产中介网站重庆企业网站seo
  • 追波设计网站东莞专业网站推广多少钱
  • 网站 设计 语言山东省住房城乡建设厅查询网站首页
  • iis7如何设置ip做网站网页游戏传奇世界网页版
  • 用cms创建自己带数据库的网站和在本机搭建网站运行平台的心得体会店名注册查询官网
  • 上传网站过程餐厅网站设计模板下载
  • 手机网站建设公司联系电话app开发人员网站
  • 营销型网站制作培训陕西网站推广费用
  • wordpress地址怎么改seo是哪里
  • 网站规划小结网站的数据库怎么做
  • wordpress代理管理多站点梅州免费建站