Rustã®ã¹ã¬ãããã¼ã«ã«å¤æ°ã«ã¤ãã¦
Rustã«ã¯2種é¡ã®ã¹ã¬ãããã¼ã«ã«å¤æ°ãããã
#[thread_local]
å±æ§
#[thread_local]
å±æ§ã¯ãæå®ãã static
ã¢ã¤ãã ãã¹ã¬ãããã¼ã«ã«ã«ããããã«LLVMã«ä¼ãããããã«ããCè¨èªã®ã¹ã¬ãããã¼ã«ã«å¤æ°ã¨åæ§ã«ãELFãªã³ã«å´ã®å¦çã«ããã¹ã¬ãããã¼ã«ã«å¤æ°ãå®ç¾ãããã
ãã®æ¹æ³ã«ã¯ä»¥ä¸ã®ãããªåé¡ç¹ãããã
- ä¸é¨ã®ãã©ãããã©ã¼ã ã§ã¯å©ç¨ã§ããªãã
- ç¾ç¶ã§ã¯ãä¸è¦ãª
Sync
ãè¦æ±ãããã - åç §ãåå¾å¾ã«ã¹ã¬ãããçµäºããã¨UBã«ãªãããå¥å ¨æ§ãä¿è¨¼ããã¦ããªãã
- 以ä¸ã®ãããªçç±ããã£ã¦ããå®å®å
¥ããã¦ããªãããããã
thread_local!
ãããã®ã§å»æ¢ãã¦ãããã®ã§ã¯ãªããã¨ããæè¦ããã
thread_local!
ãã¯ã
thread_local!
ãã¯ãã¯ãã¹ã¬ãããã¼ã«ã«å¤æ°ã«å®å
¨ã«ã¢ã¯ã»ã¹ããããã®ã©ããã¼ãçæãããä¾ãã°ã
thread_local! { static X : Cell<u32> = Cell::new(0); }
ã¨æ¸ããããå®éã«ã¯ä»¥ä¸ã®ãããªã¢ã¤ãã ãçæãããã
static X : LocalKey<Cell<u32>> = LocalKey { ... };
thread_local!
ã¯ããã¨ãã¨N:Mã®ä¸¦è¡æ§ã¢ãã«ãæ¡ç¨ãã¦ããRustãã°ãªã¼ã³ã¹ã¬ãããå»æ¢ãã¦1:1ã«ç§»è¡ããã«ããããã¿ã¹ã¯ãã¼ã«ã«å¤æ°ãæä¾ããããã® local_data!
ãæ´çããå½¢ã§ã§ãããã®ã§ãããç¾å¨ã®ãã¤ãã£ãã¹ã¬ããã®è¦³ç¹ãããããã®ãã¯ã以ä¸ã®ç¹ã§åªãã¦ããã¨ãããã
- åç
§ã®æå¹æéã
LocalKey::with
ã«ããå¶éããããããå¥å ¨æ§ã®åé¡ã解決ããã¦ããã - ELFãªã³ã«ã«ä¾åããã¹ã¬ãããã¼ã«ã«é åãæä¾ããã¦ããªãå ´åããOSéä¾åã®ä»£æ¿æ段ãå©ç¨ãããã
ãªãã¸ã§ã¯ããã¨ã®ã¹ã¬ãããã¼ã«ã«å¤æ°
以ä¸ã¨ã¯å¥ã®è©±ã ãããªãã¸ã§ã¯ããã¨ã«ã¹ã¬ãããã¼ã«ã«ãªãã£ã¼ã«ããæã¤å¿
è¦ãããå ´å㯠thread_local
crate ã¨ããã©ã¤ãã©ãªã使ããããã ã
ã¾ã¨ã
Rustã§ã¹ã¬ãããã¼ã«ã«å¤æ°ã使ãã¨ãã¯ã thread_local!
ãã¯ãã使ãã®ãæã¾ããããã㯠LocalKey
ã¨ããå®å
¨ã§ç§»æ¤æ§ã®é«ãã©ããã¼ãæä¾ãã¦ããã