Multithreaded programming (7)-From CreateThread [continued v.]

Source: Internet
Author: User
Tags bool thread

function CreateThread(
 lpThreadAttributes: Pointer; {安全设置}
 dwStackSize: DWORD;
 lpStartAddress: TFNThreadStartRoutine;
 lpParameter: Pointer;
 dwCreationFlags: DWORD;
 var lpThreadId: DWORD
): THandle; stdcall;

The first parameter of the CreateThread lpthreadattributes is a pointer to the tsecurityattributes structure, which is usually set to nil, which means there is no access restriction; The structure is defined as:

//TSecurityAttributes(又名: SECURITY_ATTRIBUTES、_SECURITY_ATTRIBUTES)
_SECURITY_ATTRIBUTES = record
 nLength: DWORD;        {结构大小}
 lpSecurityDescriptor: Pointer; {默认 nil; 这是另一个结构 TSecurityDescriptor 的指针}
 bInheritHandle: BOOL;     {默认 False, 表示不可继承}
end;
//TSecurityDescriptor(又名: SECURITY_DESCRIPTOR、_SECURITY_DESCRIPTOR)
_SECURITY_DESCRIPTOR = record
 Revision: Byte;
 Sbz1: Byte;
 Control: SECURITY_DESCRIPTOR_CONTROL;
 Owner: PSID;
 Group: PSID;
 Sacl: PACL;
 Dacl: PACL;
end;

Complicated enough, but we don't need to set them in multithreaded programming, mostly using the default settings (that is, assigning to nil).

I think it is necessary to understand at this moment: the establishment of system kernel objects generally have this attribute (tsecurityattributes);

In the next multi-threaded topic to use some kernel objects, as well as the first inventory, when you encounter this attribute to a nil can, no longer bother.

{建立事件}
function CreateEvent(
 lpEventAttributes: PSecurityAttributes; {!}
 bManualReset: BOOL;
 bInitialState: BOOL;
 lpName: PWideChar
): THandle; stdcall;
{建立互斥}
function CreateMutex(
 lpMutexAttributes: PSecurityAttributes; {!}
 bInitialOwner: BOOL;
 lpName: PWideChar
): THandle; stdcall;
{建立信号}
function CreateSemaphore(
 lpSemaphoreAttributes: PSecurityAttributes; {!}
 lInitialCount: Longint;
 lMaximumCount: Longint;
 lpName: PWideChar
): THandle; stdcall;
{建立等待计时器}
function CreateWaitableTimer(
 lpTimerAttributes: PSecurityAttributes; {!}
 bManualReset: BOOL;
 lpTimerName: PWideChar
): THandle; stdcall;

The above four system kernel objects (event, mutex, signal, timer) are the means of thread synchronization, from this also can see the complexity of processing thread synchronization; But this is not all, Windows Vista began to add Condition variables (conditional variable), Slim reader-writer Locks (read-write lock) and other synchronization means.

However, the simplest, lightest (fastest) synchronization means criticalsection (critical area), but it does not belong to the system kernel object, of course, there is no handle, no tsecurityattributes This security attribute, which also causes it can not be used across processes; However, when writing multiple threads generally do not have to cross the process ah, so criticalsection should be the most common means of synchronization.

Next time, start learning multithreading synchronization.

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.