C + + Multithreading

Source: Internet
Author: User

Multithreading is a special form of multitasking, and multitasking allows the computer to run two or more programs at the same time (two).

Two types of multitasking: process-based and thread- based ;

Process-based multitasking is the concurrent execution of programs ;

Thread-based multitasking is the concurrent execution of fragments of the same program ;

A multithreaded program contains two or more parts that can run at the same time, each of which is called a thread, and each thread defines a separate execution path;

Create thread : ( function returns 0 when creation is successful)

#include <pthread.h>

Pthread_create (thread, attr, Start_routine, arg)//Pthread_creadte Create a new thread and let it execute

Parameters: Thread: Pointer to the threading identifier;

attr: An opaque Property object that can be used to set thread properties;

Start_routine: The thread runs the start address of the function and executes as soon as the thread is created;

ARG: The argument that runs the function, which must be passed by casting the reference as a pointer to the void type;

To terminate a thread :

#include <pthread.h>

Pthread_exit (status)//Pthread_exit () for the display to exit a thread;

  

to pass parameters to a thread :

To Connect and detach a thread :

Pthread_join (thread_id, status)//blocks the calling program until the specified thread_id thread terminates;

Pthread_detach (thread_id)

When a thread is created, one of its properties defines whether it is connected (joinable) or separable (detached).

Only threads that are defined as being connected can be connected when they are created;

If a thread is created and is defined as separable, it can never be connected;

C + + Multithreading

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.