Home > Software engineering >  C will be how a member function runs in a thread
C will be how a member function runs in a thread

Time:11-19

Recently wrote A project, such as A class member function, this member function to do some heavy things such as an HTTP request, I call this
in thread AMember function is not reality, to this member function is assigned to a thread to run inside, what's the elegant way?

For example,
The class ClassA obj.
Obj. Fun ()//the fun is the HTTP request or some other heavy things to perform a few seconds, I want to put the thread execution,

I now use method is not good, such as a thread pool has a thread is idle, I members in class defines a static function
Static int threadFun (ClassA * self)
{
The self - & gt; Fun ();
}
So to perform, encapsulation is bad, such as I to perform fun2 () and want to go alone to write a threadFun2, very ugly,
Who have a better way?

Originally wanted to use assembly code to ecx then call, and this pointer is unreliable different compiler thiscall calling convention is different also, in the aspect of c + + are there ways to do?

CodePudding user response:

consider using class function pointer

CodePudding user response:

All thought of assembly directly use ecx assignment, class a function pointer, the thread parameters up or structure, all the pointer to the inside,

CodePudding user response:

The function or to write one,
Fubctions into an array,
In the class have a index m_funidx
Static int threadFun (ClassA * self)
{
The self - & gt; Funs [m_funidx];
}

CodePudding user response:

You can define some callback function, the callback function and the related CONTEXT CONTEXT parameter encapsulation, as the parameter passed to the thread function threads,

CodePudding user response:

Static int threadFun ()
{
ClassA * self=new ClassA ();
The self - & gt; Fun ();
}
This set is not good encapsulation?

CodePudding user response:

 class IRunnable 
{
Public:
Virtual void the Run ()=0;
};

The class CThread
{
Public:
CThread (IRunnable * run);
~ CThread ();

Public:
Void the Start ();
Void WaitFinish ();

Public:
IRunnable * m_run=NULL;
HANDLE m_hThread=NULL;
};



The building Lord have understand callbacks and virtual function?
So, the above interface can do it,

CodePudding user response:

 # include "stdafx. H" 
# include "Thread. H"



CThread: : CThread (IRunnable * run)
{
M_run=run;
}

CThread: : ~ CThread ()
{
: : CloseHandle (m_hThread);
}

The static DWORD WINAPI ThreadFunc (LPVOID param)
{
Param CThread * thread=(CThread *);
If (thread - & gt; M_run!=NULL)
{
Thread - & gt; M_run - & gt; The Run ();
}
return 0;
}


Void CThread: : Start ()
{
If (m_hThread==NULL)
{
DWORD dwThreadID=0;
M_hThread=: : CreateThread (NULL, 0, ThreadFunc, this, 0, & amp; DwThreadID);
}
}

Void CThread: : WaitFinish ()
{
: : WaitForSingleObject (m_hThread, INFINITE);
}

CodePudding user response:

https://blog.csdn.net/xuan_xuan_2/article/details/89299048

Under reference QThreadPool, and measures for the implementation of QRunnable
Actually this is common in the Java design patterns,,,

CodePudding user response:

Haven't junction post?
Enclose a list, with each request to this list

Threads inside the

While (1)
{
If (threadover)
break;

Int res=waitforsingleobject (newmsgevent, waittime);
According to the res judgment if not object_0
Then the newmsg without destroyed by other local
The likelihood is timeout, or directly break end thread

Resetevent (newmsgevent);
//to handle
ClassA * self=list. The getmsg ();
List. Remove (slef);
Self. Dosomething ();

The list is empty, the new information such as direct stuck to
Waittime=1;
{if (list. Isnotempty ())
//otherwise direct return to overtime, and then processing
Waittime=0;
}

Afraid of data volume high CPU can sleep once more, under normal circumstances feel not necessary, according to the actual demand,
}


Request came, do the following processing

ClassA * self=createnewrequest ();
List. The add (slef);
Setevent (newmsgevent);

Logic is simple, the thread is good writing,
Need to pay attention to the problem is that the list you need to do to thread mutex, otherwise multi-threaded insert, delete will go wrong,
Handle other events/the creation of a global variable is small dishes,

CodePudding user response:

The c + + 11 closure can be implemented easily and can even use generic thread pool https://github.com/gdlxSong/GdlLib/tree/master/gdltool/threadpool


In addition, c + + 11 standard library provides threading library, you can use the STD: : thread,
Example:

The class Test {
Public:
Void the run (int) {}
}

The main () {
Test the Test;
STD: : thread th (& amp; The Test: : run, & amp; The test, 20);
//...
Th. The join ();
}

  • Related