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

旅游网站前台模板wordpress成品图

旅游网站前台模板,wordpress成品图,网站怎么做排名优化,烟台门户网站建设在实际项目开发中#xff0c;难免需要用到全局变量#xff0c;比如全局配置信息#xff0c;全局内存池等#xff0c;此类数据结构可能在多处需要被使用#xff0c;保存为全局变量可以很方便的进行修改与读取。 在Rust中#xff0c;如果只是读取静态变量是比较简单的难免需要用到全局变量比如全局配置信息全局内存池等此类数据结构可能在多处需要被使用保存为全局变量可以很方便的进行修改与读取。 在Rust中如果只是读取静态变量是比较简单的比如全局变量是一个usize或者 str等类型的值。如果全局变量是需要初始化产生的就比较复杂了比如解析一个配置文件然后把配置文件中的内容赋给全局变量。由于全局变量要被修改这个全局变量得是可变的也就是说产生了全局可变变量而这种方式违反了Rust的设计原则。 一般方法 struct Config {id: u64, }impl Config {fn new() - Config {Config {id: 0,}} }lazy_static::lazy_static! {static ref CACHE: MutexConfig 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: OnceLockRwLockAppData 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: RwLockT1, }static APP_SHARE: OnceLockAppData 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.hkea.cn/news/14559843/

相关文章:

  • 优化网站标题绍兴大公司有哪些
  • 网站开发常用图标 图像设计图纸网站
  • 中国有没有做的好的网站推广app用什么平台比较好
  • seo网站排名优化教程现在最好的营销方式
  • 景安网站广州番禺营销型网站建设
  • 校园网站开发技术海宁营销型网站设计
  • 网站建设流程精英ui界面设计分析
  • 重庆白云seo整站优化信息流推广
  • 有了域名怎么建设网站外贸网站响应式
  • 速贝网站友情链接怎么做上海搜索优化推广哪家强
  • 专做女裤有哪些网站网站开发项目的需求分析
  • 郑州seo技术博客网站做优化的好处
  • 株洲做网站 省心磐石网络用户体验好网站
  • 西安网站开发有哪些公司台州网站开发
  • 济南做网站最好的公司网站开发外包公司坑
  • 摄影网站网址大全易思企业网站管理
  • 了解网站建设的流程建设网站可以赚钱吗
  • 成都网站建设爱特通wordpress 怎么加入插件
  • 广州房地产网站建设高端品牌化妆品排行榜前十名
  • 有什么做图文长图的网站吗网站开发工具特点总结
  • 智能网站推广软件手表网站
  • 做网站服务防止做网站的人修改数值
  • 网站建设的分工wordpress 用户 插件
  • 厦门移动网站建设哪家专业国内有哪些顶尖的设计公司
  • 宣传 网站建设和政务公开庆阳网站设计厂家
  • 购物网站建设思路网站建设中 页面
  • 电子商务网站设计流程做视频采集网站犯法
  • pw网站更换域名苏州优化外包
  • 什么软件可以找做网站的如何做电影网站
  • 域名申请好怎么做网站建筑室内设计公司排名