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

wordpress前端页面我们seo

wordpress前端页面,我们seo,网站建设流程教程,wordpress建站小百科在实际项目开发中,难免需要用到全局变量,比如全局配置信息,全局内存池等,此类数据结构可能在多处需要被使用,保存为全局变量可以很方便的进行修改与读取。 在Rust中,如果只是读取静态变量是比较简单的&…

在实际项目开发中,难免需要用到全局变量,比如全局配置信息,全局内存池等,此类数据结构可能在多处需要被使用,保存为全局变量可以很方便的进行修改与读取。

在Rust中,如果只是读取静态变量是比较简单的,比如全局变量是一个usize或者& str等类型的值。如果全局变量是需要初始化产生的就比较复杂了,比如解析一个配置文件,然后把配置文件中的内容赋给全局变量。由于全局变量要被修改,这个全局变量得是可变的,也就是说产生了全局可变变量,而这种方式违反了Rust的设计原则。

一般方法

struct Config {id: u64,
}impl Config {fn new() -> Config {Config {id: 0,}}
}lazy_static::lazy_static! {static ref CACHE: Mutex<Config> = Mutex::new(Config::new());
}fn func1() {CACHE.lock().unwrap().id = 1;
}fn func2() {CACHE.lock().unwrap().id = 2;
}fn func3() -> u64 {return CACHE.lock().unwrap().id;
}fn main() {func1();let id1 = func3();println!("id1 = {}", id1);func2();let id2 = func3();println!("id2 = {}", id2);
}

这种方法一般可以满足需求,但是需要一开始就将变量初始化。
如果有未初始化的字段,可以使用Option等类型搞定。

更好的办法

use std::{sync::{OnceLock, RwLock},thread,time::Duration,
};struct AppData {count: i64,name: String,ptr: usize,
}static APP_SHARE: OnceLock<RwLock<AppData>> = OnceLock::new();fn th_loop1() {let app = APP_SHARE.get().unwrap();loop {{let app2 = app.read().unwrap();println!("th 1 read: {}", app2.count);if (app2.count > 60) {break;}}thread::sleep(Duration::new(1, 0));}println!("th 1 end!!");
}fn th_loop2() {let app = APP_SHARE.get().unwrap();loop {{let mut app2 = app.write().unwrap();app2.count += 1;println!("th 2 write: {}", app2.count);if (app2.count > 80) {break;}}thread::sleep(Duration::new(1, 0));}println!("th 2 end!!");
}fn main() {// 在这里初始化let app = APP_SHARE.get_or_init(|| {RwLock::new(AppData {count: 12 * 4 + 5,name: "abc".to_string(),ptr: (0xFF002403) as usize,})});{let app2 = app.read().unwrap();println!("init ok: {} {} {}", app2.count, app2.name, app2.ptr);}// 线程里面使用let t1 = thread::spawn(th_loop1);let t2 = thread::spawn(th_loop2);t1.join().unwrap();t2.join().unwrap();println!("===============");
}

这里的 AppData 一开始没有初始化,在程序运行时才进行。
能更好适应一般的全局变量需求!!

读多写少的情况,也可以这样:

use std::{cell::{Cell, RefCell},default,sync::{Arc, OnceLock, RwLock},thread,time::Duration,
};
struct T1 {a: i64,b: i64,
}
struct AppData {count: i64,name: String,ptr: usize,count2: RwLock<T1>,
}static APP_SHARE: OnceLock<AppData> = OnceLock::new();fn th_loop1() {let app = APP_SHARE.get().unwrap();loop {{let c2 = app.count2.read().unwrap();println!("th 1 read: {} {}", app.count, c2.a);if (app.count > 60 || c2.a > 10) {break;}}thread::sleep(Duration::new(1, 0));}println!("th 1 end!!");
}fn th_loop2() {let app = APP_SHARE.get().unwrap();loop {{let mut c2 = app.count2.write().unwrap();c2.a += 1;println!("th 2 write: {} {}", app.count, c2.a);if (app.count > 80 || c2.a > 20) {break;}}thread::sleep(Duration::new(1, 0));}println!("th 2 end!!");
}fn main() {// 在这里初始化let app = APP_SHARE.get_or_init(|| AppData {count: 12 * 4 + 5,name: "abc".to_string(),ptr: (0xFF002403) as usize,count2: RwLock::new(T1 { a: 1, b: 2 }),});{println!("init ok: {} {} {}", app.count, app.name, app.ptr);}// 线程里面使用let t1 = thread::spawn(th_loop1);let t2 = thread::spawn(th_loop2);t1.join().unwrap();t2.join().unwrap();println!("===============");
}
http://www.hengruixuexiao.com/news/47922.html

相关文章:

  • 百度上如何做企业网站网络营销好不好
  • 常州网站建设百科现在怎么做网络推广
  • 网站建设属于什么职位沈阳专业关键词推广
  • 网站营销信息长春做网站推广的公司
  • 三河市建设局网站免费推广论坛
  • 山东省建设厅网站维护电话百度官方电话
  • 建设部网站不支持360软文营销范文100字
  • 做公司网站自助快速建站
  • 网站如何做担保交易百度运营优化师
  • 网站滚动效果怎么做最新新闻热点话题
  • 网店装修教程免费模板优化网络推广外包
  • 关于网站建设的通知搜索引擎优化的核心及内容
  • 用vs2012做asp网站在线外链工具
  • 网站跟别的做的一样的长沙seo优化服务
  • c#网站开发 pdfseo导航
  • 市场营销毕业后做什么工作怎么优化网站性能
  • 怎么设网站网店怎么推广和宣传
  • 济南网站建设和维护中国免费网站服务器下载
  • 八戒八戒在线观看免费完整版seo和sem的区别是什么?
  • 怎么看一个网站做没做竞价关键词优化骗局
  • 广东潮阳疫情天津网站优化
  • 宁波网站推广网站优化百度建站云南服务中心
  • 微信哪里可以做视频网站最新战争新闻事件今天
  • 作网站关键词优化举例
  • 百度推广要自己做网站吗网站设计模板
  • 企业网站系统知名品牌营销案例100例
  • 梅州市住房和城乡建设局网站长沙靠谱seo优化价格
  • 网站自己备案seo整体优化
  • 国家标准化建设委员会官方网站北京专业seo公司
  • 网站怎么加友情链接深圳网站页面设计