Introduction to multi-thread programming

Source: Internet
Author: User

Introduction to multi-thread programming

<Pre name = "code" class = "cpp"> the first time I involved multi-threaded programming in C language, I got started with it. I used only java Runnable multi-threaded programming. This time, we can regard the screen as a resource which is shared by two threads,
# Include <iostream> # include <windows. h> using namespace std; dword winapi Fun (LPVOID lpParamter) {while (1) {cout <"Fun display! "<Endl; Sleep (1000); // 1000 indicates one second} int main () {HANDLE hThread = CreateThread (NULL, 0, Fun, NULL, 0, NULL ); closeHandle (hThread); while (1) {cout <"main display! "<Endl; Sleep (2000);} return 0;} // multiple consecutive blank lines or line breaks can be seen as a resource, this resource is shared by two threads. When the Fun function outputs the Fun display! Then, // The endl will be output (that is, clearing the buffer zone and wrapping it. here we don't need to understand what the buffer zone is), but the main function // gets the chance to run it, at this time, the Fun function gave the CPU to the main function before it could output a line feed, and then the main function // is directly in Fun display! And output main display!
// Solution 1: # include <iostream> # include <windows. h> using namespace std; dword winapi Fun (LPVOID lpParamter) {while (true) {cout <"Fun display! \ N "; Sleep (1000) ;}int main () {HANDLE hThread = CreateThread (NULL, 0, Fun, NULL, 0, NULL); CloseHandle (hThread ); while (true) {cout <"Main display! \ N "; Sleep (2000);} return 0 ;}
// Solution 2: Achieve alternate operation. This time, the problem is fundamentally solved, and multi-threaded shared resources are implemented. # include <iostream> # include <windows. h> using namespace std; HANDLE hMutex; dword winapi Fun (LPVOID lpParamter) {while (true) {WaitForSingleObject (hMutex, INFINITE); cout <"Fun display! "<Endl; Sleep (1000); ReleaseMutex (hMutex) ;}} int main () {HANDLE hThread = CreateThread (NULL, 0, Fun, NULL, 0, NULL ); hMutex = CreateMutex (NULL, FALSE, "aa"); cout /*

1 CreateThread function null, 0, fun, null, 0, null
HANDLE CreateThread (
LPSECURITY_ATTRIBUTESlpThreadAttributes, // thread Security Attribute
DWORDdwStackSize, // stack size
LPTHREAD_START_ROUTINElpStartAddress, // thread function
LPVOIDlpParameter, // thread Parameter
DWORDdwCreationFlags, // thread creation attribute
LPDWORDlpThreadId // thread ID
);
2 HANDLE (HANDLE): it is not a pointer, but a unique index of a process.
HANDLE is a concept in the Windows operating system. In a Windows program, there are various resources (such as Windows, icons, and cursors). The system allocates memory for these resources and returns the ID number indicating these resources, handle. Handle refers
A unique index of a core object in a process, not a pointer. Content identified by the handle due to address space restrictions
The process is invisible and can only be maintained by the operating system through the process handle list. Handle list: each process must create
Handle list, which points to various system resources, such as semaphores, threads, and files. All threads in the process can access
These resources.
3. dword winapi explanation: WINAPI is a call Convention. WINAPI is a _ stdcall, DWORD 32-bit system.
Dword winapi ThreadProc (
LPVOID lpParameter // thread data
);
4 Create an exclusive resource, parameter: null, false, "resource name"
HANDLE CreateMutex (
LPSECURITY_ATTRIBUTES lpMutexAttributes, // SD
BOOL bInitialOwner, // initial owner
Lptstr lpName // object name
);
The first parameter is not used. It can be set to NULL. The second parameter specifies whether the resource belongs to the process where it was created and the third parameter specifies the Resource Name.
Example: HANDLE hMutex = CreateMutex (NULL, TRUE, "screen ");
This statement creates a resource named screen and belongs to the process that created it.
5. release an exclusive resource:
BOOL ReleaseMutex (
HANDLE hMutex // handle to mutex
);
Once a process releases the resource, it no longer belongs to it. If you need to use the resource again, you need to apply for it again.

6. resource application function: the first parameter specifies the handle of the requested resource, and the second parameter is generally set to INFINITE.
DWORD WaitForSingleObject (
HANDLE hHandle, // handle to object
DWORD dwMilliseconds // time-out interval
);
It indicates that if the resource is not applied for, it will wait for the resource. If it is specified as 0, it indicates that the resource will be returned once it is not obtained. You can also specify how long it will be returned, in the unit of 1‰ seconds.
*/

Introduction to multi-thread programming

A small part of the multi-threaded programming knowledge C ++ does not have a special book to talk about ....
 
I want to learn C ++ multi-threaded programming. What are the best teaching materials?

First, C ++ must be ready.

C language programming
Book.jqcq.com/product/472414.html
Language is a widely used computer language at home and abroad. It is a programming tool that computer applications should master. This book comprehensively and systematically introduces the C language programming technology and its related theories. It is a good tutorial that enables readers to fully master the C language programming technology. Based on the requirements, the author not only introduces the traditional programming methods of C language in a simple way, but also introduces the plotting functions of C language and some application development examples. The book contains novel content and structures...

C ++ programming style
Book.jqcq.com/product/620299.html
Programming-related content, such as methods to increase code readability, maintainability, scalability, and execution efficiency. The sample code in this book is extracted from the actual program, integrating the author's actual development experience. Explains how to write code correctly and avoid some common mistakes and traps, and provides many practical programming rules to quickly improve the reader's C ++ programming skills. This book is concise and has rich examples. Is it suitable for programming? ...

C ++ programming ideology 2nd volume: practical programming technology
Book.jqcq.com/product/413352.html
Programming technology and best practices, in-depth exploration of exception handling methods and exception security design; introduces the modern usage of C ++ strings, input and output streams, STL algorithms, containers and templates, including template meta-programming, explaining the difficulties of Multi-inheritance, displaying the actual use of RTTI, describing the typical design mode and its implementation, special introduction to the multi-thread processing programming technology that is considered to be one of the features of the Standard C ++ next edition, and provides the latest research results? ...

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.