lock 关键字的使用和 C# 5.0 的新异步功能
在使用异步(AsyncCtpLibrary.dll)调用的方法中是否仍然需要对 SQL Compact 数据库等资源使用 lock 关键字?据我从 Anders 的演讲中了解到,异步处理全部发生在同一个线程中,所以它们不应该是需要的,还是我错了?目前我在互联网上找不到任何相关信息。
谢谢
Is it still necessary to use the lock keyword on resources like SQL Compact database in methods called with async (AsyncCtpLibrary.dll)? As i understand from the talk given by Anders, the async processing all happens within the same thread, so they shouldn't be a need for it, or am I wrong? I cannot find any info on this anywhere on the internet at the moment.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AFAIK 异步基于 TPL 和任务 - 所以不,它们不会每次都在同一个线程上运行(或继续在同一个线程上),是的,您在设计时仍然必须考虑到并发性。异步只会帮助您以更好的方式将各个部分组合在一起。
需要明确的是:方法中的所有内容(如果仅启动一次)都将一次在一个线程中运行,但如果您共享资源,您将不得不考虑锁定或其他同步方法,就像您过去一直所做的那样。
如果您可以使用不可变数据 - 这样您就可以将所有这些都精简到最低限度,但您始终必须记住您的进程将在许多线程上运行(想到了 UI 调度)。
AFAIK async is based on the TPL and Tasks - and so no they won't be running on the same thread every time (or continue on the same thread) and yes you have to design with concurency in mind still. Async only helps you to put the pieces together in a much nicer way.
To be clear: everything inside your methods (if started only once) will run in a thread at a time but if you share resources you will have to think on locking or other synchronization methods just as you used to do all the time.
If you can go for immutable data - this way you can strip all this to a mere minimum, but you allway have to remember that your processes will run on many threads (dispatch for UI comes to mind).