Create a thread and pass the parameter afxbeginthread

Source: Internet
Author: User

From: http://baike.baidu.com/view/994702.html? Fromtaglist

Cwinthread * afxbeginthread (afx_threadproc pfnthreadproc,

Lpvoid pparam,

Int npriority = thread_priority_normal,

Uint nstacksize = 0,

DWORD dwcreateflags = 0,

Lpsecurity_attributes lpsecurityattrs = NULL); // used to create a worker thread

Returned value: a pointer to the thread object of the new thread.

Pfnthreadproc: Specifies the thread entry function. The declaration must be as follows: uint mythreadfunction (lpvoid pparam );

Pparam: The parameter passed into the thread. Note that its type is lpvoid, so we can pass a struct into the thread.

Npriority: Specifies the priority of a thread. It is generally set to 0 so that it has a common priority with the main thread.

Nstacksize: Specifies the stack size of the newly created thread. If it is 0, the newly created thread has a stack of the same size as the main thread.

Dwcreateflags: Specifies the mark of the thread after the thread is created. You can specify two values:

Create_suincluded: After the thread is created, it will be suspended until the call: resumethread 0: starts running after the thread is created.

Lpsecurityattrs: point to a security_attributes struct, which is used to indicate the security of the newly created thread. If it is null,

The newly created thread has the same security as the main thread.

To end a thread within the thread, you can call afxendthread within the thread.

Two ways to end a thread

When you use a thread in the background to print some images, sometimes you want to stop after printing a part. How can you stop the thread.

Two ways to end a thread

1: This is the simplest way, that is, to complete the thread function execution. At this time, the thread ends normally. It returns a value. Generally, 0 indicates that the thread ends successfully,

Of course, you can define your desired value to indicate that the thread is successfully executed. Calling afxendthread in the thread will directly end the thread, and all the resources of the thread will be recycled.

2: If you want another thread B to end thread A, you need to pass the information in the two threads.

Whether it is a worker thread or interface thread, if you want to get the result after the thread ends, you can call:

: Getexitcodethread Function Cwinthread * afxbeginthread (afx_threadproc pfnthreadproc,

Lpvoid pparam,

Int npriority = thread_priority_normal,

Uint nstacksize = 0,

DWORD dwcreateflags = 0,

Lpsecurity_attributes lpsecurityattrs = NULL

); // Used to create a worker thread

Returned value: a pointer to the thread object of the new thread.

Pfnthreadproc: Specifies the thread entry function. The declaration must be as follows: uint mythreadfunction (lpvoid pparam );

Pparam: The parameter passed into the thread. Note that its type is lpvoid, so we can pass a struct into the thread.

Npriority: Specifies the priority of a thread. It is generally set to 0 so that it has a common priority with the main thread.

Nstacksize: Specifies the stack size of the newly created thread. If it is 0, the newly created thread has a stack of the same size as the main thread.

Dwcreateflags: Specifies the mark of the thread after the thread is created. You can specify two values:

Create_suincluded: After the thread is created, it will be suspended until the call: resumethread

0: start running after the thread is created.

Lpsecurityattrs: point to a security_attributes struct, which is used to indicate the security of the newly created thread. If it is null,

The newly created thread has the same security as the main thread.

To end a thread within the thread, you can call afxendthread within the thread.

Two ways to end a thread

When you use a thread in the background to print some images, sometimes you want to stop after printing a part. How can you stop the thread.

 

Note:

A thread function can only accept one parameter. Therefore, if multiple parameters are to be passed, multiple parameters must be encapsulated (closed to struct or class ).

 

 

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.