Thread suspension with multiple threads

Source: Internet
Author: User
Original Author name Qu Yang

Body
【CodeFor details, see the appendix]

1. Safely abort a thread

There are two methods to safely abort a thread:

1.1 thread function return
Use the return statement directly.
1.2 call the afxendthread Function
The function is defined as follows:
Void afxendthread (uint nexitcode );
The nexitcode parameter is the exit code of the thread.
Note: afxendthread must be called within the thread. If the thread ends in another thread, it must be implemented through thread communication. For example, if thread 1 is terminated in Thread 0 and the exit code is 100, thread communication is performed using the event method. The Code is as follows:

Thread 0 code:
Setevent (m_pthread1-> m_hendevent );
: Waitforsingleobject (m_pthread1-> m_hthread, infinite );

Thread 1 code:
Int cthread1: Run ()
{
While (true)
{
......
If (waitforsingleobject (m_hendevent, 0) = wait_object_0)
{
Trace ("thread 1 exits \ n ");
: Afxendthread (100 );
}
}
}

2. Obtain the exit code of the thread.

call the getexitcodethread function to obtain the exit code of the thread.
Function Definition:
bool getexitcodethread (handle hthread, lpdword lpexitcode);
the hthread parameter is the thread handle (input parameter ), the lpexitcode parameter is the pointer to the exit code address (output parameter ).
If the thread is running, getexitcodethread uses the still_active identifier (defined as 0x103) to enter the address. Otherwise, use the exit code to enter the address. The Code is as follows:

If (getexitcodethread (hthread, & dwexitcode ))
{
If (dwexitcode = still_active)
{
Trace ("the thread is running \ n ");
}
Else
{
Trace ("Exit thread, exit code: % d \ n", dwexitcode );
}
}

By default, when a cwinthread thread is terminated, the thread object will be revoked (m_bautodelete = true), which means that the thread handle (cwinthread member variable m_hthread) cannot be obtained ), because the cwinthread object does not exist. To avoid this problem, you can use either of the following methods:

2.1 set m_bautodelete
Set the cwinthread member variable m_bautodelete to false, so that the thread object will not be revoked when the thread is suspended, so the thread handle can still be obtained.

M_pthread1 = (cmythread1 *) afxbeginthread
(Runtime_class (cmythread1 ),
Thread_priority_above_normal,
0,
Create_suincluded );

M_pthread1-> m_bautodelete = false;

M_pthread1-> resumethread ();

Note: Because the thread object cannot be automatically revoked when the thread is suspended, it must be revoked by the user.

If (m_pthread1! = NULL)
{
Delete m_pthread1;
M_pthread1 = NULL;
}

2.2 save thread handle
After a thread is created, call the: duplicatehandle function to copy its handle to other variables. In this way, even if the thread object is automatically revoked when the thread is suspended, the thread handle can still be obtained.

M_pthread2 = (cmythread2 *) afxbeginthread
(Runtime_class (cmythread2 ),
Thread_priority_above_normal,
0,
Create_suincluded );

M_pthread2-> m_bautodelete = true; // Default Value

: Duplicatehandle (getcurrentprocess (),
M_pthread2-> m_hthread,
Getcurrentprocess (),
& M_hthread2,
0,
False,
Duplicate_same_access );

M_pthread2-> resumethread ();

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.