.net多线程学习笔记1

来源:互联网 发布:linux进程通信 编辑:程序博客网 时间:2024/03/28 22:22

1.什么是process和thread?

2.什么是appdomain?

3.什么是thread local storage及相关操作?

4.在.net framework中如何启动一个进程?

5.callbacks回调

6.参考资料和实验代码下载

 


1.了解.net framework下的process和thread

 

windows下通过启动任务管理器能够看到当前计算中正在运行的application应用程序的列表,如下图所示:

 

下图中显示了当前计算机中所有的进程信息:

 

上面的图片展示了下面一个事实,application中不止包含一个process,一个process可以包含几个thread,同时一个process中的所有进程thread共享操作系统分配给该进程的内存、时间片等资源,process和thread的关系如下图所示:

 

 


2.什么是appdomain?

2.1AppDomain简介

进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象。一个process可以存在多个AppDomain,如下图所示:

 

2.2 存在了process之后为什么还需要使用AppDomain?

  • Since more than one AppDomain can exist in a process, we get some benefits. For example, until we had an AppDomain, processes that needed to access each other's data had to use a proxy, which introduced extra code and overhead. By using an AppDomain, it is possible to launch several applications within the same process. The same sort of isolation that exists with processes is also available for AppDomains. Threads can execute across application domains without the overhead of inter process communication. This is all encapsulated within the AppDomain class. Any time a namespace is loaded in an application, it is loaded into an AppDomain. The AppDomain used will be the same as the calling code unless otherwise specified. An AppDomain may or may not contain threads, which is different to processes.
  • "I have previously needed to execute code in a separate AppDomain for a Visual Studio add-in that used Reflection to look at the current project's DLL file. Without examining the DLL in a separate AppDomain, any changes to the project made by the developer would not show up in Reflection unless they restarted Visual Studio. This is exactly because of the reason pointed out by Marc: once an AppDomain loads an assembly, it can't be unloaded."

  • an AppDomain can be use to load an assembly dynamically, and the entire AppDomain can be destroyed without affecting the process. I think this illustrates the abstraction/isolation that an AppDomain gives us.简单的讲AppDomain提供了动态加载assembly能力

2.3.AppDomain简单使用示例

 

2.3.1设置并读取AppDomain中的数据

 

2.3.2测试一个process中存在多个AppDomain,但是各个AppDomain中数据相互隔离的

 


3.什么是thread local storage及相关操作?

 

3.1thread local storage简介

 

cpu在一个时间点上只能执行一个程序,那么当一个程序的时间片到了之后,操作系统需要将这个程序换出,去执行另外的程序,那么这个将要被换出的程序在被移除cpu的使用权之前,需要将当前运行的信息保存起来,这就是tls的作用。在msdn中存在下面的对于tls的解释:

 

"Threads use a local store memory mechanism to store thread-specific data. The Common Language Runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.

If the named slot does not exist, a new slot is allocated. Named data slots are public, and can be manipulated by anyone."

 

3.2 相关操作 

3.2.1 thread local storage数据存储隔离

 

3.2.2使用ThreadStaticAttribute属性保证每个进程的数据唯一

 

 

 


 

4.在.net framework中如何启动一个进程?

 

在.net framework中启动一个进程是比较简单,示例如下,分别是传递参数和不传递参数的情况:

 

不带参数:

 

 

带参数:

 


5.线程间的同步

 

这里使用一个timer来模拟线程之间的同步。

 

 

 


 

7.参考资料和实验代码下载

 

参考资料:

http://www.codeproject.com/KB/threads/ThreadingDotNet.aspx#Processes

http://msdn.microsoft.com/en-us/library/ms684841(v=vs.85).aspx

 

 

原创粉丝点击