Windows core programming --- thread Creation

Source: Internet
Author: User

In windows core programming, a process is submitted as a thread container and provides space for thread execution. It does not perform any operations.

The WINODWS system provides the following APIs for creating threads.

1
HANDLE CreateThread (
2
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
3
DWORD dwStackSize, // initial thread stack size
4
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
5
LPVOID lpParameter, // argument for new thread
6
DWORD dwCreationFlags, // creation flags
7
LPDWORD lpThreadId // pointer to receive thread ID
8
);
However, developers generally do not use this API to create threads. Instead, they use the _ beginthreadex function. If MFC is used, AfxBeginThread can be used to create a thread.

 

The process of creating a thread is as follows:

 

 

After the thread kernel object is created, it starts to allocate the thread's stack memory, which is allocated from the space of the process. Therefore, the thread does not have its own memory space. The system writes pvParam and pfnStartAddrj to the stack of the thread.

Each thread has its own set of c p u registers, that is, the context of the thread. This context reflects the status of the c p u register of the thread during the last running of the thread. The c p u registers of threads are stored in a c o n t e x t structure (defined in the Wi n n t. h header file. The c o n t e x t structure itself is contained in the kernel object of the thread. When the thread is scheduled again, the thread can continue to be executed by reading the context.

 

The previous simple example shows how to create a windows Thread.


01
# Include <stdio. h>
02
# Include <string>
03
# Include <windows. h>
04
# Include <process. h>
05
# Include <iostream>
06
Using namespace std;
07
 
08
 
09
Class ThreadX
10
{
11
Private:
12
Int loopStart;
13
Int loopEnd;
14
Int dispFrequency;
15
 
16
Public:
17
String threadName;
18
 
19
ThreadX (int startValue, int endValue, int frequency)
20
{
21
LoopStart = startValue;
22
LoopEnd = endValue;
23
DispFrequency = frequency;
24
}
25
 
26
Static unsigned _ stdcall ThreadStaticEntryPoint (void * pThis)
27
{
28
ThreadX * pthX = (ThreadX *) pThis;
29
 
30
Printf ("loopStart: % d, loopEnd: % d, dispFrequency: % d \ n", pthX-> loopStart,
31
PthX-> loopEnd,
32
PthX-> dispFrequency );
33
 
34
Int I = 0;
35
For (I = pthX-> loopStart; I <pthX-> loopEnd; I + = pthX-> dispFrequency)
36
{
37
Printf ("I = % d \ n", I );
38

39
}
40

41
Return 1; // the thread exit code
42
}
43
};
44
 
45
 
46
Int main ()
47
{
48
ThreadX * threadX1 = new ThreadX (0,100, 5 );
49
 
50
HANDLE hThread1;
51
Unsigned uiThread1ID;
52
 
53
HThread1 = (HANDLE) _ beginthreadex (NULL, // security
54
0, // stack size
55
ThreadX: ThreadStaticEntryPoint,
56
ThreadX1, // arg list
57
Create_suincluded, // so we can later call ResumeThread ()
58
& UiThread1ID );
59
 
60
If (hThread1 = 0)
61
Printf ("Failed to create thread 1 \ n ");
62
 
63
ThreadX1-> threadName = "thread1 ";
64
ResumeThread (hThread1 );
65
 
66
WaitForSingleObject (hThread1, INFINITE );
67
CloseHandle (hThread1 );
68

69
Delete threadX1;
70
ThreadX1 = NULL;
71
Printf ("Primary thread terminating. \ n ");
72
System ("pause ");
73
}

Author: "The blog of West Kunlun"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.