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

做好网站维护管理推广计划步骤

做好网站维护管理,推广计划步骤,WordPress会员中心模板,抖音代运营成本预算目录 条件变量 条件变量初始化和唤醒 键盘触发条件变量唤醒线程demo 条件变量的等待 条件变量定时等待demo 条线变量实现多线程间的同步 条件变量 条件变量是为了控制多个线程的同步工作而设计的 比如说一个系统中有多个线程的存在但有且仅有一个线程在工作&#xff0c…

目录

条件变量

条件变量初始化和唤醒

键盘触发条件变量唤醒线程demo

条件变量的等待

条件变量定时等待demo

条线变量实现多线程间的同步


条件变量

条件变量是为了控制多个线程的同步工作而设计的

比如说一个系统中有多个线程的存在但有且仅有一个线程在工作,我们需要等待这个线程执行完任务之后然后唤醒另一个线程执行另外一个任务,

那这个时候“正在工作的这个线程执行完任务”就是一个条件变量,等这个条件变量触发之后正在工作的线程就会休眠,然后新的线程会启动。
 

条件变量初始化和唤醒

#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); 

cond:条件变量

attr:属性默认为 NULL

返回值: 成功 0                                                                                                                                                  失败 -1

#include <pthread.h>

//随机唤醒一个等待的线程                                                                                                                int pthread_cond_signal(pthread_cond_t *cond);

//唤醒所有正在等待的线程                                                                                                                 int pthread_cond_broadcast(pthread_cond_t *cond);    

键盘触发条件变量唤醒线程demo
#include <stdio.h>
#include <pthread.h>pthread_cond_t cond;
pthread_mutex_t mutex;int n = 0;void *task(void *arg)
{while (1){printf("%ld 线程等待条件\n", pthread_self());pthread_cond_wait(&cond, &mutex);if (n == 1){n = 0;printf("%ld 线程被唤醒,执行任务\n", pthread_self());}}
}int main()
{// 初始化锁与条件变量pthread_cond_init(&cond, NULL);pthread_mutex_init(&mutex, NULL);// 创建一个线程pthread_t tid;pthread_create(&tid, NULL, task, NULL);pthread_create(&tid, NULL, task, NULL);pthread_create(&tid, NULL, task, NULL);pthread_create(&tid, NULL, task, NULL);while (1){printf("1.唤醒随机一个线程  2.唤醒所有线程\n");int n = 0;scanf("%d", &n);if (n == 1){pthread_cond_signal(&cond); // 唤醒随机一个}else if (n == 2){pthread_cond_broadcast(&cond); // 唤醒所有}}
}
条件变量的等待

#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);

cond:条件变量

mutex:互斥锁

abstime:定时器

返回值:


条件变量定时等待demo
#include <stdio.h>
#include <pthread.h>
#include <time.h>pthread_cond_t cond;void *task(void *arg)
{while (1){printf("输入任意键唤醒线程\n");getchar();pthread_cond_signal(&cond);}
}int main()
{// 0.初始化互斥锁pthread_mutex_t mutex;pthread_mutex_init(&mutex, NULL);// 1.初始化条件变量int ret = pthread_cond_init(&cond, NULL);if (ret < 0){perror("初始化条件变量失败\n");}// 创建一个线程pthread_t tid;pthread_create(&tid, NULL, task, NULL);// 2.开启定时等待while (1){// 设置时间struct timespec ts;//clock_gettime()获取时间函数//CLOCK_REALTIME:系统实时时间,可以从网络同步,也可以用户自行更改clock_gettime(CLOCK_REALTIME, &ts); #if 0  //timespec结构体,tv_sec为秒
struct timespec
{__time_t tv_sec;    /* Seconds.  秒*/__syscall_slong_t tv_nsec;  /* Nanoseconds.纳秒*/      
}           
#endifts.tv_sec += 5;                     // 时间增加 5 秒printf("开启定时等待5秒\n");//pthread cond wait函数的返回值为0,代表成功等待条件变量并且收到了通知。//如果返回值是一个非零值,则表示函数运行出现了错误//需要根据错误码进行处理。int ret = pthread_cond_timedwait(&cond, &mutex, &ts);printf("等待结束 %d\n", ret);  //超时返回值110}
}

条线变量实现多线程间的同步

利用条件变量使三个线程轮流运作,线程1执行完之后执行线程2,线程2执行完之后执行线程3,线程3执行完之后重新执行线程1.

#include <stdio.h>
#include <pthread.h>// 定义三个条件
pthread_cond_t first;  // 条件1
pthread_cond_t second; // 条件2
pthread_cond_t third; // 条件3pthread_mutex_t mutex;void *task(void *arg)
{while (1){pthread_cond_wait(&third, &mutex);printf("线程1运行\n");pthread_cond_signal(&first);}
}void *task1(void *arg)
{while (1){pthread_cond_wait(&first, &mutex);printf("线程2运行\n");pthread_cond_signal(&second);}
}void *task2(void *arg)
{while (1){pthread_cond_wait(&second, &mutex);printf("线程3运行\n");pthread_cond_signal(&third);}
}int main()
{// 初始化条件变量与线程锁pthread_mutex_init(&mutex, NULL);pthread_cond_init(&first, NULL);pthread_cond_init(&second, NULL);pthread_cond_init(&third, NULL);// 创建三个任务线程pthread_t tid;pthread_create(&tid, NULL, task, NULL);pthread_t tid1;pthread_create(&tid1, NULL, task1, NULL);pthread_t tid2;pthread_create(&tid2, NULL, task2, NULL);while (1){printf("输入任意键线程开始工作\n");getchar();pthread_cond_signal(&third);}
}

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

相关文章:

  • 知名网站定制报价星子县网站建站公司
  • 做视频网站要什么软件网站开发赚钱的方法
  • 做原油的网站wordpress主题龙
  • 做门户网站最重要的是什么wordpress新闻模板
  • 石家庄网站建设培训班淘宝关键词排名是怎么做的
  • 网站开发php是什么意思wordpress木马乐主题
  • iis7.5 网站配置手机在线做ppt模板下载网站
  • ps网站导航怎么做传媒公司签约主播合同
  • 中山品牌网站建设网络设计与制作课程
  • 管理员怎么看网站在线留言网站推广模式
  • 哪些网站是动态的好看的 网站正在建设中源码
  • 青海 住房和建设厅网站东莞互联网公司排名
  • 网站建设与制作wordpress 头部导航
  • 泰安商城网站开发设计公司网站建设推进表
  • 白酒招商网站大全wordpress登录入口
  • 哪些网站做商标注册杭州vi设计价格
  • 浙江省城乡住房建设部网站wdcp 安装wordpress
  • 导航网站没有内页没有了微信小程序是什么
  • wordpress小图标网站松江新城做网站
  • 长春网站建设厂家游戏开发属于什么行业
  • 自己做网站和推广wordpress调用头像
  • 济南建网站价格消费品展南京公司网站建设
  • 四川省建设厅网站为什么打不开北京最新新闻
  • 北京建设工程协会网站wordpress如何发布
  • 湖北网站建设找哪家郑州淘宝网站建设
  • 网站上做推广方案wordpress 用户遍历
  • 公司网站制作定制接广告的平台推荐
  • 深圳做网站有哪些如皋网站制作
  • 社区服务呼叫系统 网站的建设青岛网站排名多少钱
  • 玉林市住房和城乡建设厅网站注册网站租空间哪里租