Simple multithreading (utilizing OD to resolve run-time errors)

Source: Internet
Author: User

Code from the "VC + + in-depth" chapter 15th "Multithreading", located on page No. 563-page 566.

The purpose of the program is to demonstrate the effect of multi-threaded operation:

#include <windows.h>#include<iostream.h>DWORD WINAPI fun1proc (LPVOID lpparameter//Thread Data);intindex =0;intMain () {HANDLE hThread1; HThread1= CreateThread (NULL,0, Fun1proc,null,0, NULL);    CloseHandle (HTHREAD1);  while(Index++ < +) cout<<"main thread is running."<<Endl; return 0;} DWORD WINAPI Fun1proc (LPVOID lpparameter//Thread Data){     while(Index++ < +) cout<<"Thread1 is running"<<Endl; return 0;} 

The runtime found that the program crashed:

Click Cancel with OD debugging:

The program was discovered because the memory that read a 0 address throws an exception. F5 Debug Programs in VC6:

The yellow arrow points to the code that references the pointer p, and P has a value of 0, which throws an exception. Right-click the open file's properties to view the filename:

is a C language file, the file name is write, the meaning of writing. What operation of our program is used to write? That's right! Use the cout to output a string to the screen.

Change the # include statement to the standard format:

#include <windows.h>#include<iostream>using namespaceStd;dword WINAPI fun1proc (LPVOID lpparameter//Thread Data);intindex =0;intMain () {HANDLE hThread1; HThread1= CreateThread (NULL,0, Fun1proc,null,0, NULL);    CloseHandle (HTHREAD1);  while(Index++ < +) cout<<"main thread is running."<<Endl; return 0;} DWORD WINAPI Fun1proc (LPVOID lpparameter//Thread Data){     while(Index++ < +) cout<<"Thread1 is running"<<Endl; return 0;}

You can run it perfectly:

However, what is this for? We just guessed the address of the problem and didn't know the principle. Try Baidu for a moment:

iostream.h and iostream are different. #include <iostream.h> is used in the old standard C + +. In the new standard, use #include<iostream>. Iostream means the input and output stream. #include <iostream> is a standard C + + header file , and any standard C + + development environment has this header file. Also note: In VC programming to add: using namespace std; The reason for this is that the header file with the suffix. h C + + standard has been explicitly not supported, the earlier implementation of the standard library functions defined in the global space, declared in the header file with the. h suffix, C + + standard in order to distinguish with C, and in order to correctly use the namespace, the header file does not use the suffix. Therefore, when using <iostream.h>, the equivalent of calling library functions in C, using the global namespace, which is the earlier C + + implementation, when using <iostream>, the header file does not define a global namespace. Namespace STD must be used so that cout can be used correctly. Relationship <string.h> is the old C header file , corresponding to the char*-based string processing function;<string> is the C + + header file that wraps the STD, corresponding to the new string class;< Cstring> is the STD version that corresponds to the old C header file. The relationship between <iostream.h> and <iostream>, similar to the <string.h> and <cstring> relationships, implements the same functionality, primarily whether the distinction between using a namespace STD is used.
The reason is that we use. h header files that are not supported by the C + + standard, where the processing functions are written in the C language, so that the char*-based processing functions are likely to have large problems in multi-threading, or <iostream> are more secure and more compliant.

Simple multithreading (utilizing OD to resolve run-time errors)

Related Article

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.