CreateThread instances of C + +

Source: Internet
Author: User

function CreateThread (
Lpthreadattributes:pointer; {Security Settings}
Dwstacksize:dword; {Stack size}
Lpstartaddress:tfnthreadstartroutine; {Entry Function}
Lpparameter:pointer; {function Argument}
Dwcreationflags:dword; {Startup options}
var lpthreadid:dword {output Thread ID}
): Thandle;                     stdcall; {return thread handle}

1. Return value: Returns the thread handle
A handle is like a pointer, but a pointer can read or write an object, and a handle simply uses the object;
Objects with handles are generally system-level objects (or kernel objects); The reason given to us is a handle rather than a pointer, with only one purpose: "Safety";
Seemingly through the handle can do a lot of things, but generally put the handle to a function (usually a system function), we also end up difficult to understand more; In fact, the system doesn't believe us.

Whether it is a pointer or a handle, is just a small piece of data in memory (general structure description), Microsoft does not expose the structure of the handle details, guess it should include: real pointer address, access permissions settings, reference count, and so on.

Since CreateThread can return a handle, the thread belongs to "kernel object".
In fact, what process does not belong to the pipeline, they are equal in the arms of the system; At the same time, the system will run each thread at the same interval, but the interval is so small that it makes us mistakenly assume that the program is running continuously.

//threadbase.h<br> #pragma once#include <windows.h>classcthreadbase{ Public: Cthreadbase (void); ~cthreadbase (void); StaticDWORD WINAPI ThreadProc (PVOID pparam); Virtual voidRun () =0; voidStart ();Private: HANDLE m_hthread; DWORD M_dwthreadid;};

//ThreadBase.cpp#include"StdAfx.h"#include"ThreadBase.h"Cthreadbase::cthreadbase (void) {M_hthread= CreateThread (NULL,0, ThreadProc, This,create_suspended,&m_dwthreadid);} Cthreadbase::~cthreadbase (void) {CloseHandle (m_hthread);} DWORD WINAPI Cthreadbase::threadproc (PVOID pparam) {if(null!=pparam) {(Cthreadbase*) Pparam)Run (); }    return 0;}voidCthreadbase::start () {resumethread (m_hthread);}

Reference documents:

Using CreateThread () to create multithreaded threads

CreateThread Usage Explanation

CreateThread instances of C + +

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.