The thread context includes CPU register values, kernel stack, user stack and an environment block in the address space of the process.
Every process have at least one thread which is called the primary thread. While compiling and linking the code the entry point of the primary thread is decided. When the Operating System runs application the main thread gets the control of the application. Now main thread is free to create any number of threads limited by the resource availability in the system.
Threads run independent of each other. This means one thread does not know that other threads are running. Also threads are preempted by OS and unpredictable. On the other hand threads of the same process share the virtual address, system resources, data and code of the process.
Sharing code does not create any problem but sharing data and resources can create problem. For example if one thread is in the middle of modifying one piece of data and then another thread gets preempted which access that data, the second thread will get inconsistent value.
Also there can be scenario where one thread need to wait for some action to be completed by another thread.
So synchronization of resource access and communication between threads are essential part of a multi-threaded applications. A thread in Windows has some attributes which are decided during the creation and also can be altered before the thread start execution. A suspended thread is the one that is not currently running. Threads can be created in the suspended state and then started later. If a thread is in the suspended state, then the call to start the thread executing is ResumeThread. It takes the handle of the thread as a parameter.
There is a SuspendThread call that will cause a running thread to be suspended. This call is expected to be used only by tools such as debugger. Suspending a running thread may lead to problems if the thread currently holds resources such as mutexes. The following code demonstrates the creation of a suspended thread and then calling ResumeThread on that thread. The code uses a call to getchar , which waits for the enter key to be pressed, to separate the creation of the thread from the act of resuming thread:.
The suspended state of the thread is handled as a counter, so multiple calls to SuspendThread need to be matched with multiple calls to ResumeThread.
Each thread of execution has associated with it a suspend count. If this count is zero, then the thread is not suspended. If it is nonzero, the thread is in a suspended state. Each call to SuspendThread increments the suspend count. Each call to ResumeThread decrements the suspend count. A suspended thread will resume only when its suspend count has reached zero. Therefore, to resume a suspended thread implies that there must be the same number of calls to ResumeThread as there have been calls to SuspendThread.
Many of the Windows API functions return handles. As we saw from the earlier discussion of type casting, these are really just unsigned integers. However, they have a particular purpose. Windows API calls that return handles have actually caused a resource to be created within the kernel space. The handle is just an index for that resource. When the application has finished with the resource, the call to CloseHandle enables the kernel to free the associated kernel space resources.
Typically, the starting address is the name of a function defined in the program code for more information, see ThreadProc. A process can have multiple threads simultaneously executing the same function. The following is a simple example that demonstrates how to create a new thread that executes the locally defined function, MyThreadFunction.
The calling thread uses the WaitForMultipleObjects function to persist until all worker threads have terminated. The calling thread blocks while it is waiting; to continue processing, a calling thread would use WaitForSingleObject and wait for each worker thread to signal its wait object.
Note that if you were to close the handle to a worker thread before it terminated, this does not terminate the worker thread. However, the handle will be unavailable for use in subsequent function calls. This routine can be called any number of times from anywhere within your code.
Here is the description of the parameters. The maximum number of threads that may be created by a process is implementation dependent. Once created, threads are peers, and may create other threads.
There is no implied hierarchy or dependency between threads.
0コメント