Methods for creating thread functions

Source: Internet
Author: User
Tags function definition

1. Thread Functions

before starting a thread, you must write a global thread function for the thread that takes a 32-bit LPVOID as a parameter and returns a uint with the structure of the thread function:

UINT threadfunction (LPVOID pparam)
{
Thread-handling code
Return0;
}

The Thread processing code section usually includes a dead loop that waits for something to happen before processing the related work:

while (1)
{
WaitForSingleObject (...,...); /or WaitForMultipleObjects (...)
Do something
}

generally speaking, the class member function of C + + cannot be used as a thread function. This is because the member functions that are defined in the class are added by the compiler to this pointer. Please see the following procedure:

#include "Windows.h"
#include
Class Exampletask
{
Public
void Taskmain (LPVOID param);
void StartTask ();
};
void Exampletask::taskmain (LPVOID param)
{}

void Exampletask::starttask ()
{
_beginthread (Taskmain,0,null);
}

int main (int argc, char* argv[])
{
Exampletask Realtimetask;
Realtimetask.starttask ();
return 0;
}

The following error occurred while compiling the program:

Error C2664: ' _beginthread ': cannot convert parameter 1 from ' void (void *) ' to ' void (__cdecl *) (void *) '
None of the functions with this name in scope match the target type

then look at the following procedures:

#include" windows.h "
#include  
class exampletask 
{ 
public: 
void Taskmain (LPVOID param);  
}; 

void Exampletask::taskmain (LPVOID param)  
{} 

int main (int argc, char* argv[])
{
Exampletask realtimetask;
_beginthread (Exampletask::taskmain,0,null);
return 0;

The program compiles with an error:

Error C2664: ' _beginthread ': cannot convert parameter 1 from ' void (void *) ' to ' void (__cdecl *) (void *) '
None of the functions with this name in scope match the target type

If you must take a class member function as a thread function, there are usually the following solutions:

(1) Declare the member function as a static type,RemoveThis pointer;

We will change the above two programs to:

#include "Windows.h"
#include
Class Exampletask
{
Public
void static taskmain (LPVOID param);
void StartTask ();
};

void Exampletask::taskmain (LPVOID param)
{}

void Exampletask::starttask ()
{
_beginthread (Taskmain,0,null);
}

int main (int argc, char* argv[])
{
Exampletask Realtimetask;
Realtimetask.starttask ();
return 0;
}
And
#include "Windows.h"
#include
Class Exampletask
{
Public
void static taskmain (LPVOID param);
};

void Exampletask::taskmain (LPVOID param)
{}

int main (int argc, char* argv[])
{
_beginthread (Exampletask::taskmain,0,null);
return 0;
}

are compiled and passed.

declaring a member function static can resolve a problem as a thread function, but it introduces a new problem, which is that the static member function can onlyAccessstatic member. One way to solve this problem is to pass the this pointer as a parameter when invoking a class static member function (thread function), and in a thread function to convert the this to a pointer to the class using the exponentially, which accesses the non-static member through that pointer.

(2) does not define a class member function as a thread function, but a threadfunction Definitiona friend function for the class. In this way, the thread function can also have the same permissions as the class member function;

We have modified the program to:

#include "Windows.h"
#include
Class Exampletask
{
Public
friend void Taskmain (LPVOID param);
void StartTask ();
};

void Taskmain (LPVOID param)
{
Exampletask * Ptaskmain = (exampletask *) param;
Referencing by Ptaskmain pointers
}

void Exampletask::starttask ()
{
_beginthread (Taskmain,0,this);
}
int main (int argc, char* argv[])
{
Exampletask Realtimetask;
Realtimetask.starttask ();
return 0;
}

(3) You can implement callbacks on non-static member functions and access non-static members, which involves some advanced techniques that are no longer detailed here.

Methods for creating thread functions

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.