大佬教程收集整理的这篇文章主要介绍了C#ThreadStatic volatile成员无法按预期工作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
无论如何,我有两个静态私有字段:
private static volatile String staticVolatileTestString = ""; [ThreadStatic] private static int threadInt = 0;
如您所见,我正在测试ThreadStaticAttribute和volatile关键字.
private static String TestThreadStatic() { // Firstly I'm creaTing 10 threads (DEFAULT_test_SIZE is 10) and starTing them all with an anonymous method List<Thread> startedThreads = new List<Thread>(); for (int i = 0; i < DEFAULT_test_SIZE; ++i) { Thread t = new Thread(delegate(object o) { // The anon method sets a newValue for threadint and prints the new value to the volatile test String,then waits between 1 and 10 seconds,then prints the value for threadInt to the volatile test String again to confirm that no other thread has changed it int newVal = randomnumberGenerator.Next(10,100); staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setTing threadInt to " + newVal; threadInt = newVal; Thread.Sleep(randomnumberGenerator.Next(1000,10000)); staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " finished: " + threadInt; }); t.Start(i); startedThreads.Add(t); } foreach (Thread th in startedThreads) th.Join(); return staticVolatileTestString; }
thread 0 setTing threadInt to 88 thread 1 setTing threadInt to 97 thread 2 setTing threadInt to 11 thread 3 setTing threadInt to 84 thread 4 setTing threadInt to 67 thread 5 setTing threadInt to 46 thread 6 setTing threadInt to 94 thread 7 setTing threadInt to 60 thread 8 setTing threadInt to 11 thread 9 setTing threadInt to 81 thread 5 finished: 46 thread 2 finished: 11 thread 4 finished: 67 thread 3 finished: 84 thread 9 finished: 81 thread 6 finished: 94 thread 7 finished: 60 thread 1 finished: 97 thread 8 finished: 11 thread 0 finished: 88
但是,我得到的是:
thread 0 setTing threadInt to 88 thread 4 setTing threadInt to 67 thread 6 setTing threadInt to 94 thread 7 setTing threadInt to 60 thread 8 setTing threadInt to 11 thread 9 setTing threadInt to 81 thread 5 finished: 46 thread 2 finished: 11 thread 4 finished: 67 thread 3 finished: 84 thread 9 finished: 81 thread 6 finished: 94 thread 7 finished: 60 thread 1 finished: 97 thread 8 finished: 11 thread 0 finished: 88
输出的第二个“一半”是预期的(我想这意味着ThreadStatic字段的工作方式与我想的一样),但似乎有一些初始输出已从第一个“半”中“跳过”.
另外,第一个“half”中的线程是乱序的,但我知道一旦调用Start(),线程就不会立即运行;但相反,内部操作系统控件将按其认为合适的方式启动线程.
编辑:不,他们不是,实际上,我只是认为他们是因为我的大脑错过了连续数字
所以,我的问题是:导致我在输出的前半部分丢失几行会出现什么问题?例如,’thread 3将threadInt设置为84’的位置在哪里?
staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setTing threadInt to " + newVal;
>线程1读取staticVolatileTestString
>线程2读取staticVolatileTestString
>线程3读取staticVolatileTestString
>线程1附加内容并写回staticVolatileTestString
>线程2附加内容并写回staticVolatileTestString
>线程3附加内容并写回staticVolatileTestString
这会导致你的线路丢失.挥发无助于此;连接字符串的整个过程不是原子的.您需要围绕这些操作使用锁定:
private static object sync = new object(); lock (synC) { staticVolatileTestString += Environment.NewLine + "\tthread " + ((int) o) + " setTing threadInt to " + newVal; }
以上是大佬教程为你收集整理的C#ThreadStatic volatile成员无法按预期工作全部内容,希望文章能够帮你解决C#ThreadStatic volatile成员无法按预期工作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。