C + + Supplements-Multithreading: the introduction of multithreading

Source: Internet
Author: User

C + + Supplements-Multithreading: the introduction of multithreading

Objective

Multithreading is an important part of programming. Multi-core era makes multithreading known as a possibility, obviously, one thing many people do, efficiency will certainly improve. Let's look at how multithreading is used in C language.

Body

Let's look at an example first

#define _crt_secure_no_warnings#include <stdio.h> #include <stdlib.h> #include <process.h> #include <windows.h>dword WINAPI run (void *p) {char *mess = (char*) p;printf ("thread%d, popup \ n", GetCurrentThreadID ()); Char threadid[20];sprintf (threadId, "thread%d", GetCurrentThreadID ()); MessageBoxA (0, mess, threadId, 0); return 0;} int main (void) {printf ("******c language multithreaded Presentation ***by david***\n"), char *mess[] = {"123", "456", "789"}; HANDLE handles[3];for (int i = 0; i < sizeof (mess)/sizeof (*mess); i++) {Handles[i] = CreateThread (NULL, 0, run, mess[i ], 0, NULL);} WaitForMultipleObjects (3, handles, 1, INFINITE); return 0;}
Run



Three windows were ejected asynchronously, and the respective thread numbers were printed. If there is no understanding of the place, the following is explained in detail:

1.handle is a handle that identifies an object in Windows with a handle. Nature is simple typedef void * HANDLE;

2.CreateThread () is used to create threads. Prototype

HANDLE WINAPI CreateThread (
Lpsecurity_attributes lpthreadattributes,//security properties for kernel objects
size_t dwstacksize,//thread stack size
Lpthread_start_routine lpstartaddress,//thread function address
LPVOID Lpparameter,//parameters passed to the thread function
DWORD dwcreationflags,//control bit
Lpdword lpthreadid//Get thread ID
);

Parameter explanation:

The first parameter is the security attribute of the thread kernel object, which is typically passed in NULL represents the use of the default settings.

The second parameter is the size of the line stacks space. Passing in 0 means using the default size (1MB).

The third parameter is the address of the thread function that the new thread executes, and multiple threads can use the same function address.

The fourth parameter is a parameter passed to the thread function. typedef void * LPVOID

The fifth parameter is used to control the creation of a thread, and 0 means that it executes immediately after creation.

The sixth parameter is the outgoing parameter, which is used to get the ID of the thread. Obviously, passing in NULL indicates that the caller does not want to know the thread's ID.

3. Declaration of the thread function. #define WINAPI __stdcall (vs2013) typedef unsigned long DWORD. Where __stdcall refers to the way a function is called in C/cpp. There are two main points: 1. The arguments are in the stack from right to left. 2. The caller is responsible for emptying the parameter stack.

4. Thread wait function

DWORD
WINAPI
WaitForMultipleObjects (
DWORD ncount,//number of kernel objects
CONST HANDLE *lphandles,//address of handle array
BOOL bWaitAll,//whether to wait for all
DWORD dwmilliseconds//wait for the maximum time, in milliseconds, infinite to indicate infinite wait
);

function function: Let the thread enter the wait-and-go state until the condition is triggered. The kernel object is in a state that is not triggered during operation until the end of execution.


Summarize:

With multi-threading, you write a thread function and then call the related function to create the thread.





Directory of this column

    • C + + Supplements directory
Directory of all content
    • CCPP Blog Directory



C + + Supplements-Multithreading: the introduction of multithreading

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.