Thread local storage

前端 未结 4 1709
陌清茗
陌清茗 2020-12-08 07:21

what is the best way to store some variable local to each thread?

4条回答
  •  青春惊慌失措
    2020-12-08 07:45

    Another option in the case that scope is an issue you can used Named Data Slots e.g.

        //setting
        LocalDataStoreSlot lds =  System.Threading.Thread.AllocateNamedDataSlot("foo");
        System.Threading.Thread.SetData(lds, "SomeValue");
    
        //getting
        LocalDataStoreSlot lds = System.Threading.Thread.GetNamedDataSlot("foo");
        string somevalue = System.Threading.Thread.GetData(lds).ToString();
    

    This is only a good idea if you can't do what James Kovacs and AdamSane described

提交回复
热议问题