0

I am looking into a MultiThreading c# tutorial and I have not understood well the entire picture regarding Processes and Threads. I have understood that Threads are within the process but it is not clear what is exactly a process ? Is an instance of a class, a particular method, and entire assembly file, what is it ? This doubt coming out when i have seen the difference between Lock and Mutex. The definition of Mutex Class is ...."A synchronization primitive that can also be used for interprocess synchronization..." and later on .... Mutex is a synchronization primitive that grants exclusive access to the shared resource to only one thread, This confuse me a bit? Am i wrong is stating than Mutex synchronizes threads from different Processes ?

Regarding ThreadPool the definition is..."A thread pool is a collection of threads that can be used to perform several tasks in the background...." this collection of threads are from same process or threads of different processes?

tshepang
  • 12,111
  • 21
  • 91
  • 136
MaxMarcucci
  • 175
  • 9
  • Check the remarks section on the MSDN page for the `Mutex` class: http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx, it should clarify some of your questions regarding the multiple definitions. You may want to check other existing questions for a compare/contrast between the various .Net synchronization mechanisms: http://stackoverflow.com/questions/301160/what-are-the-differences-between-various-threading-synchronization-options-in-c – Preston Guillot Oct 16 '13 at 16:17
  • extreamly summarized a process is (short and easy answer - a running program,more formal - is an OS concept used to describe a set of resources and necessary memory allocations used by a running application),a thread is(ilustratively speaking) a path of execution within a process,but you should dig more to get a cleaner grasp. – terrybozzio Oct 16 '13 at 16:22

3 Answers3

9
  • A Process is a collection of AppDomain's. 99% of all programs have only 1 AppDomain
  • An AppDomain is a collection of Threads and static variables
  • A Thread is a thing that executes a series of instructions (your code), you may have multiple series of instructions running at the same time (multiple threads) in a AppDomain
  • A ThreadPool is just a collection of threads for short lived tasks that get recycled after they are finished instead of being "deleted" because it is less taxing on system resources to do that.
  • Mutex is used to make sure that two threads (from any Process) do not use a shared resource at the same time if that resource is not programmed to handle multiple threads accessing it at the same time. This can be accomplished by either waiting for the other person to finish then continuing on (this is called blocking) or not doing the operation at all.
  • A lock is like a "simple mutex" that only does the "wait till the other person is finished" method. It is easier to use but only works within a single AppDomain, so it does not provide cross AppDomain or cross Process protection.

Lastly, not directly asked but:

  • An Assembly is a collection of code bundled in to a single file, that single file can have code that is running multiple AppDomains or have multiple files (think dll's and a single exe) all running in a single AppDomain. There is no relationship between Assemblies and Threads/AppDomains/Processes (well there is a relationship between AppDomains and Assemblies, that is how it handles the separate static variables. However, for everything you care about, there is no relationship).
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
1

A process is a memory instance of a program. If you run Notepad multiple times, you'll see in Processes under Task Manger multiple instances of Notepad.exe.

A thread is a sequence of instructions. Multiple threads can be executed in parallel for a single process. For example in MS Word, different threads could be responsible for spelling/grammar checking, autosave, ... and handling the UI.

Jerry
  • 4,258
  • 3
  • 31
  • 58
0

A process is an OS level bucket within which your application will reside.

Threads will be contained within your process but are largely managed by the OS.

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

Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35