C + + Windows multithreaded __c++

Source: Internet
Author: User

Reproduced from: http://blog.chinaunix.net/uid-26275986-id-3886498.html


A process is an important concept in a system, which simply means a running program, but a program represents a static instruction code. A process consists of a system-managed kernel object and the address space of the resource that holds the program. Kernel objects are managed by the system, so applications cannot be accessed directly; The address space contains all the resources required for the program to run, such as executable modules, DLLs, code and data, and dynamically allocated stacks and heaps. In fact, the process is the container of the resources that the program runs. But the process only provides a place for the execution of the program, and it is the thread that actually implements the process. Each process starts by automatically creating a thread as the main thread to create additional threads. Threads are more lightweight than processes, and the main link is:
1. Threads are created in the process's address space, have full access to process resources, share resources between multithreading, and can communicate freely;
2. The line Chengjian immediately also has its own kernel object and the line stacks, but is not the address space;
3. When multitasking is generally required, we recommend threading implementations, because creating a process requires allocating address space, and switching between processes is also inconvenient, that is, the use process overhead is high;
First, create multithreading
After a brief look at the concept of process and thread, let's see how to create a thread in a program. The Windows SDK provides us with a dedicated function for creating threads: CreateThread ()

Click (here) to collapse or open HANDLE CreateThread (
Lpsecurity_attributes LPSA,
DWORD Cbstack,
Lpthread_start_routine Lpstartaddr,
LPVOID Lpvthreadparam,
DWORD Fdwcreate,
Lpdword Lpidthread
); The first parameter is a security attribute structure that primarily controls whether the thread handle can be inherited by the process's subprocess, which is not inherited by default when null is used; If you want to inherit a thread handle, you need to set the struct body to initialize the bInheritHandle member of the struct to true;
The size of the thread initial stack represented by -2-cbstack, if 0 is used to indicate the default size initialization;
-3-LPSTARTADDR indicates where the thread starts, that is, the function code the thread is going to execute, which is somewhat analogous to the use of a callback function;
-4-lpvthreadparam is used to receive the parameters of the thread procedure function and can be set to NULL when it is not needed;
-5-fdwcreate represents the flag when the thread is created, create_suspended indicates that the thread was created pending execution, must call ResumeThread before it can be executed, 0 indicates that the thread was created immediately after the
-6-lpidthread is used to hold the ID of the thread;
Understanding the use of the CreateThread function, let's take a look at an example and actually experience it. The following example opens a thread loop output information, and we can view the result:

Click (here) to collapse or open//multithread

#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace Std;

DWORD WINAPI Fun1proc (LPVOID lpparameter);


int main ()
{

int j = 0;

HANDLE hthread_1 = CreateThread (null, 0, fun1proc, NULL, 0, NULL);
CloseHandle (hthread_1);

while (j + + < 1000)
cout << "Mainthread is running for" << "the" << J << "Times" <<endl;

System ("pause");
return 0;


}


DWORD WINAPI Fun1proc (LPVOID lpparameter)
{
int i = 0;
while (i++ < 1000)
cout << "Thread 1 is running for" << "the" << I << "Times" <<endl;

return 0;
Because we use System ("pause") commands, we do not need the sleep () command to see the alternate execution of the main thread and the split thread:


Second, the problem of multithreading
The above program we just build a new thread, if we build two. Below we simulate a train ticketing model, thread 1 and thread 2 are also responsible for ticketing, the main thread is responsible for the platform. Thread 1 and thread 2 respectively access the global variable tickets, output its value as a ticket and then reduce its value by one, until the "sold out" all tickets:

Click (here) to collapse or open//multithread

#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace Std;

DWORD WINAPI Fun1proc (LPVOID lpparameter);
DWORD WINAPI Fun2proc (LPVOID lpparameter);

int tickets = 100;
int main ()
{
HANDLE hthread_1 = CreateThread (null, 0, fun1proc, NULL, 0, NULL);
HANDLE hthread_2 = CreateThread (null, 0, fun2proc, NULL, 0, NULL);
CloseHandle (hthread_1);
CloseHandle (hthread_2);
System ("pause");
return 0;


}


DWORD WINAPI Fun1proc (LPVOID lpparameter)
{
while (true)
{if (Tickets > 0)
{
Sleep (10);
cout << "Thread 1 Sell ticket:" <<tickets--<<endl;
}
Else
Break }
return 0;
}

DWORD WINAPI Fun2proc (LPVOID lpparameter)
{

while (true)
{

if (Tickets > 0)
{
Sleep (10);
cout << "Thread 2 Sell ticket:" <<tickets--<<endl;
}
Else
Break

}
return 0;
}

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.