我想了解如何编写线程安全代码.
例如,我在我的游戏中有这个代码:
bool _done = false;
Thread _thread;
// main game update loop
Update()
{
// if computation done handle it then start again
if(_done)
{
// .. handle it ...
_done = false;
_thread = new Thread(Work);
_thread.Start();
}
}
void Work()
{
// ... massive computation
_done = true;
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,可能会发生主游戏线程和我_thread可以拥有自己的缓存版本_done,并且一个线程可能永远不会_done在另一个线程中看到更改?
如果可能,如何解决?
是否可以解决,只应用volatile关键字.
或者是有可能读取和写入,通过价值Interlocked的类似的方法Exchange和Read?
如果我包围_done读写操作lock (_someObject),需要我使用Interlocked什么东西来防止缓存?
编辑1
我读了一本书"通过C#第四版CLR".我无法理解一个陈述:
因此,例如,如果您有以下代码行:
Run Code Online (Sandbox Code Playgroud)FileStream[] fsArray;那么当CLR创建的
FileStream[]类型,它会导致这种类型的自动实现IEnumerable<FileStream>,ICollection<FileStream>以及IList<FileStream>接口.此外,该FileStream[]类型也将实现的接口的基本类型:IEnumerable<Stream>,IEnumerable<Object>,ICollection<Stream>,ICollection<Object>,IList<Stream>,和IList<Object>.
我用这段代码测试了这个语句:
FileStream[] fsArray = new FileStream[0];
string s = null;
foreach (var m in fsArray.GetType().GetInterfaces())
s += m.ToString() + Environment.NewLine;
Run Code Online (Sandbox Code Playgroud)
结果,我有这个:
System.ICloneable
System.Collections.IList
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
System.Collections.Generic.IList`1[System.IO.FileStream]
System.Collections.Generic.ICollection`1[System.IO.FileStream]
System.Collections.Generic.IEnumerable`1[System.IO.FileStream]
System.Collections.Generic.IReadOnlyList`1[System.IO.FileStream]
System.Collections.Generic.IReadOnlyCollection`1[System.IO.FileStream]
Run Code Online (Sandbox Code Playgroud)
没有执行IEnumerable<Stream>和其他人!我在某处弄错了吗?或杰弗里里希特犯了错误?
此外,我认为这是无意义的.因为Arrays支持协方差.
如果我在内部包含外部引用,where predicate则内存不会获得释放.
List<object>如果我写where predicate这样的话,我想说我有一个:
List<object> myList = new List<object>();
...
myList.add(object);
...
Expression<Func<object,bool>> predicate = p => myList.Contains(p);
Run Code Online (Sandbox Code Playgroud)
即使我做了myList = null或者predicate = null,它也没有释放记忆.
我已经List<object> itemsource绑定了DataGrid.我也使它的ItemSource为null,处理DataGrid,DataGrid为null..我还用ANTS Memory Profiler 7.4分析了这个问题.它也告诉我,因为wherepredicate它是持有参考.
如果我改变我wherepredicate像这样dispose(),那么内存得到释放.
Expression<Func<object,bool>> predicate = p => p.id == 0;
Run Code Online (Sandbox Code Playgroud)
这意味着删除参考WherePredicate.
在我的工作项目中,导航不起作用。
我需要在 VSCode 中打开的文件夹是:c:\repoSVN\prj_ai_prjs_rc\. 我要加载的解决方案是:c:\repoSVN\prj_ai_prjs_rc\implementation\KRN\KRN.sln
当我看到 OmniSharp 日志时,我发现它加载了错误的解决方案:
Starting OmniSharp server at 2017-11-27 09:57:44
Target: c:\repoSVN\prj_ai_prjs_rc\.build\02_Aure.Product.sln
Run Code Online (Sandbox Code Playgroud)
没有错误。只需加载解决方案及其项目。没有什么关于KRN.sln
有时当我移动鼠标(围绕角色旋转相机)时,我的角色会旋转.但我没有鼠标任何字符旋转.这是我的旋转代码,仅依赖于键盘输入(我从标准统一样本中获取此代码):
private void ConvertMoveInput()
{
Vector3 localMove = transform.InverseTransformDirection(_moveInput);
_turnAmount = Mathf.Atan2(localMove.x, localMove.z);
_forwardAmount = localMove.z;
}
Run Code Online (Sandbox Code Playgroud)
我发现有时当我移动鼠标Mathf.Atan2时返回数字PI,即使两个参数仍然等于零.这是怎么回事?

我的演示项目在这里.
我正在尝试这样做:
with
function add_fnc(p_id number) return number
is
begin
return p_id + 1;
end;
insert into temp_table
(
select add_fnc(1) from dual
);
Run Code Online (Sandbox Code Playgroud)
但它显示编译错误:
ORA-00928:缺少SELECT关键字
有没有办法使用带有insert语句的CTE函数?