Programming experience (four) - "Use the member function of the class as a thread execution function"

xiaoxiao2021-03-06  18

Programming experience (four)

Use the member function of the class as a thread execution function

2005-03-29

Use the member function of the class as a thread execution function

2005-03-29

Use the member function of the class as a thread execution function

2005-03-29

Servers: Yang Yanqing E-mail: Blankmanattomdotcom, Http://blog.9cbs.net/blankman/archive/2005/03/29/333031.aspx

The project I have made is under Linux. A part of the project needs to have a "guard" thread in a class, check the status of certain parts in the class and the corresponding reflection, which introduces this part of the idea. 1. The thread is first introduced here about threads, but not detailed, please refer to "POSIX Thread Detailed" if you need to know more. l pthread_create () function [declaration] #include int pthread_create (pthread_t * thread, pthread_attr_t * attr, void * (* start_routine) (void *), void * arg); Parameters thread - type is pthread_t Pointer. The pthread_t type is defined in pthread.h, commonly referred to as "thread id" (abbreviated as "TID"). It can be considered to be a thread handle; attr - used to define certain properties of the thread. Generally only need to set this parameter to null, use the default attribute; start_routine - the function name called when the new thread starts. The function called VOID * as a parameter, and the type of return value is also VOID *, which indicates that any type of data can be passed to the new thread, and the new thread can also return any type of data; arg - to thread Parameters of the parameters in the function of the call. [Return value] = 0 - successfully created thread; ≠ 0 - creation failed. Such as: eagain - defined in errno.h, the "# define eagain 11" system does not have enough resources to build threads (possibly that your thread has exceeded the maximum number of threads, this quantity is pointed out by this macro definition); ENOMEM - Defined in Error.h, "# define enomem 12" is insufficient. L pthread_cancel () [Declaration] #include int pthread_cancel (pthread_t thread); [Parameter] thread - The target thread to be canceled, the function sends a CANCEL request to the thread pointed to by Thread. [Return value] = 0 - perform success; ≠ 0 - perform failed. Such as: esrch - did not find the thread pointed to by PTHREAD. 2, my problem introduces the related knowledge of threads. Here, according to my thinking process, I hope to be inspired by everyone. The project I have to do is done such a function, each instance has its own independent space, "daemon" handling the interior of the class. There is also data interaction between them, and the corresponding processing is performed after receiving the data. The class has a buffer receiving data, preventing processing is not timely, the "daemon" discovers that there is data in the buffer, and it is not empty.

First analyze the problem, we need to create a class, a thread variable in a class, the function to be executed, and other related information, the class declaration is as follows: #include #include class device {private : Pthread_t m_thread; void * tfun (void * arg); void destroy (void); public: device (args); ~ device (); int init (args); ......}; 【Description】 (1 "ARGS" representative parameters in the statement, the same below. (2) This project does not use an exception mechanism to determine the function execution based on the return value. The constructor does not return a value and cannot reflect the state of the run, and the establishment of the thread is possible to generate an error, so introducing the init () function. Put the operations that do not generate errors (such as 初, etc.) in the constructor, put all the operations that may generate errors in this function so that the initialization result of the program can be known.

In this step, I will undermine the following implementation statement: int device :: init (args) {int errno = 0; errno = pthread (& m_thread, null, tfun, null); if (errno) {goto End; } ... end: destroy (); return errno;} [Description] (1) Now basically does not advocate using a goto statement, but I think it can be used in one way to use the goto statement, so you will not jump. Make people confused, but also improve efficiency, can also enable functions to have "single-inverting list" (only one return statement, rather than returning error value directly); (2) may see it at a glance There is a problem here, but I didn't see it at the time. After compile: main.cpp: in Member Function `Int Device :: init (int) ': main.cpp: xx: no matches converting function` tfun' to Type` void * (*) 'main.cpp: xx: Candidates Are: void * Device :: tfun (void *) I thought about a long time, the parameter changed, or compiled, it was only known, non-static Member function cannot be used as a function of thread! It seems that there are two ways to think about it. I have two ways: First, the member function declares as a static function; second, write a non-member function void * function as a thread execution function. But these two mid methods have their own problems. The first method, the static function cannot access non-static variables, although he is a member function, but the access is not all variables. The second method, you need to pass the plug of the instance to the function, but the function only has access to the public variable. For the first solution, we need to adjust the following: (1) The function declares that the static function; (2) Picking the pointer THIS of the instance in the form of a void * when the function tfun () is called; (3) The internal increase of the function TFUN () "Device * mthis = (device *) arg;". For the second solution, we need to adjust the following: (1) declare the function as a friend function of the class device (otherwise the access is not a non-public member variable); (2) Put the present example when the function fun () is called The pointer THIS is passed in the form of void *; (3) Internal adding pointer conversion statement "Device * MTHIS = (Device *) arg;" in the internal increase of the function FUN ().

I want to say that the first program structure is compact, it is more convenient, and the finishing code is as follows: #include #include class device {private: void destroy (void) PTHREAD_T M_THREAD; static void * tfun (void * arg); public: device (void); ~ device (); int it (int Args);}; int device :: init (int args) {int errno = 0; Errno = pthread_create (& m_thread, null, tfun, this); if (errno) {goto end;} ... end: return errno;} void * Device :: tfun (void * arg) {device * mthis = (Device *) arg; ...} to this, the problem is resolved. Previous: Programming experience (3) - "C, C ] Problems with pointer plus 1 (2004-11-23)" Next: Disclaimer: Original, copyright, if you need to reprint, please indicate the source. http://blog.9cbs.net/blankman/archive/2005/03/29/333031.aspx PS: Now 9CBS editing function is getting more and more difficult to use -_- !! talk later

转载请注明原文地址:https://www.9cbs.com/read-41334.html

New Post(0)