This article supporting source code
First, the preface
I am studying the communication of threads, but there is so little information on this, there is no way I have to chew MSDN, but MSDN seems not very clear. So I wrote an example to look at the learning of multithreaded programming played a role in the casting of jade Brick. It's always easier to learn with an understandable example. I've been reviewing these algorithms recently, so I've written these algorithms here as examples of threads. At the same time, some general methods of sorting algorithm are also described.
This example uses multithreading to sort the data using different sorting algorithms, and each thread uses a different algorithm. A quick sort quicksort is used in the main thread, and the other four algorithms create four child threads, which are sorted in child threads. Because each thread calls the function Printresult to output the result to the monitor, the different threads scramble to output to the monitor, so that the output of the different threads is mixed together, so that the thread has to output one after another. That is, the printresult must be mutually exclusive.
System. To be mutually exclusive control, you must use the amount of mutex control such as event, mutex, Crititicalsection, semaphore, and so on. This example can be used with event, mutex, crititicalsection, and you can use one of the mutexes to test the code as prompted to modify it. I wrote the example does not use MFC, uses the SDK the WINAPI, if uses the MFC to have the slight difference, but the principle is same. MFC also divides the threads into user interface threads and worker threads, essentially the difference between the user interface thread and the worker thread is that the base class that the user interface thread is inheriting has implemented the message loop, and MFC helps you do a lot of message processing and interface control work.
Introduction to WINAPI Threading control functions: For more information, check out MSDN
1.1 Thread Build functionHANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
// 安全属性结构指针,可为NULL
DWORD dwStackSize,
// 线程栈大小,若为0表示使用默认值
LPTHREAD_START_ROUTINE lpStartAddress,
// 指向线程函数的指针
LPVOID lpParameter,
// 传递给线程函数的参数,可以保存一个指针值
//所以,线程函数的参数只能是一个32位值
//而且线程函数返回值也有规定,必须是unsigned long
DWORD dwCreationFlags,
// 线程建立是的初始标记,运行或挂起
LPDWORD lpThreadId
// 指向接收线程号的DWORD变量
);
1.2 Critical resource control function:
1 Creation of Event objects
The purpose of an event object is to send a public event signal to a thread, using the CreateEvent function to create:
HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes,
// 安全属性结构指针,可为NULL
BOOL bManualReset,
// 手动清除信号标记,TRUE在WaitForSingleObject后必须手动调
//用RetEvent清除信号。
//若为FALSE则在WaitForSingleObject后,系统自动清除事件信号
BOOL bInitialState, // 初始状态,TRUE有信号,FALSE无信号
LPCTSTR lpName // 信号量的名称,字符数不可多于MAX_PATH
//如果遇到同名的其他信号量函数就会失败,如果遇到同类信号同名
//也要注意变化
);2 the creation of mutually exclusive amount
The effect of a mutex is to ensure that only one thread can get a mutex at a time and continue execution, using the CreateMutex function to create: HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,
// 安全属性结构指针,可为NULL
BOOL bInitialOwner, // 当前建立互斥量是否占有该互斥量
//TRUE表示占有,这样其他线程就不能获得此互斥量也就无法进入由
//该互斥量控制的临界区。FALSE表示不占有该互斥量
LPCTSTR lpName // 信号量的名称,字符数不可多于MAX_PATH
//如果遇到同名的其他信号量函数就会失败,如果遇到同类信号同名
//也要注意变化
); 3 initialization of critical region signals
Must be initialized before use
VOID InitializeCriticalSection(
LPCRITICAL_SECTION lpCriticalSection // 临界区变量指针
); 4) Blocking function
If the waiting semaphore is not available, the thread is suspended until the signal available thread is awakened, and the function automatically modifies the signal, such as event, after which the thread is awakened
Event signal will become no signal, Mutex, semaphore, etc. will also change. We use the WaitForSingleObject function to wait for the signal, and if we have to wait for multiple signals, we can use the Waitformutipleobject function.
DWORD WaitForSingleObject(
HANDLE hHandle, // 等待对象的句柄
DWORD dwMilliseconds // 等待毫秒数,INFINITE表示无限等待
);Second, the example explanation