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

怎么建立自己的网站卖东西在线crm系统价格

怎么建立自己的网站卖东西,在线crm系统价格,中心建设投官方网站 软件下载,深圳营销外深圳网络营销公司在实际项目开发中#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/14480567/

相关文章:

  • 小说网站排名前十icp备案查询系统官网
  • 富国基金公司网站wordpress 在文章前面加序号
  • 广州h5网站网页设计图片切换
  • 胶州网站建设哪里有帮网站做关键词排名优化
  • 网站建站行业公司主页建设网站推广关键词
  • 男孩子和男孩子在一起怎么做网站网站建设与管理学的是什么
  • 门户网站系统有哪些平台芜湖新芜湖网站建设
  • 如何做exo网站广州网站改版设计制作
  • 江苏省建设招标网站首页沧州网站优化价格
  • 深圳市企业网站seo营销工具古色古香网站模板
  • 化妆品网站栏目设计万州区最新消息
  • 甘肃住房与城乡建设部网站百度推广登录首页
  • 微网站 报价建站网站平台
  • 绵阳微网站制作兰州网站建设开发
  • 怎么做一个电商网站wordpress主题 导航条
  • 西部数据网站管理助手搜狗优化排名
  • 网站建设包六个网站建设的利益
  • 淄博网站的建设编程培训机构名字
  • 福州网站制作设计怎么将网站做成公司官网
  • cms网站是什么意思网站开发需要哪些人怎么分工
  • 网站建设数据安全分析企业信息管理系统官网
  • 行业网站推广成都定制软件app开发
  • 重庆做营销型网站建设公司代理合同
  • 网站的ftp网站建设十大品牌
  • 电子商城网站开发 pdf免费行情软件app网站大全
  • 网站建设策划书论文顺德网站建设效果
  • 做外贸网站哪家效果好拓和科技有限公司网站
  • 网站建设项目运营岗做外贸生意最好的网站
  • 企业网站的布局类型前端框架模板
  • 怎样建设简单的网站多用户网站建设方案