A kind of communication means of customized multithreading control
Technical field
The invention belongs to the communication means that program communication method more particularly to a kind of multithreading are managed.
Background technique
Multi-thread programming is a kind of software design that the several operation systems such as Windows/Linux/vxWorks are generally supported
The design method that a variety of development language platforms such as method and C/C++, Java, C# are supported.In each generic operation system of C/C++ level
Management control, data communication between enterprising line journey of uniting, there is similar a concept and mode, for example, mutual exclusion, locking, synchronization and
Wait etc..In JAVA since the multithreading API of language level supports that multi-thread design becomes more simple, JAVA Multi-thread control
The granularity of management is thicker compared with C/C++, thus flexibility is unlike the latter.
On each type operating system, using C/C++ carry out multithread programming thread management-control method, especially two
The synchronization of father and son's cross-thread progress different code sections.It is realized usually using lock mechanism, signal lamp, it can be on the windows
The switching and control of code execution flow are carried out using message.But these mechanism generally use kernel objects, external variable, in resource
It is not brief enough in consumption, code wrap;The message mechanism of windows be it is non real-time, sometimes will lead to the excessively long time
Delay.
Summary of the invention
In order to overcome, the kernel resources consumption of the communication means of existing multithreading control is larger, code wrap is not brief, real
The poor deficiency of when property, the present invention provides a kind of consumption of reduction kernel resources, code wrap is more brief, real-time is good certainly
The communication means of the multithreading control of definition.
The technical solution adopted by the present invention to solve the technical problems is:
A kind of communication means of customized multithreading control, the communication means include the following steps:
1) an argument structure object is designed for the entrance function of sub thread, embedded service parameter and two orders refer to
Needle individual event;Embedded service parameter is required for normal service design function, and command pointer individual event is between father and son's thread
Control management designed by;
2) it defined in father's thread, initialize 2 local variables, address is opened as uplink, down order parameter pointer
Mover thread;
3) change local variable value in father's thread, the local variable includes down order parameter, which changes
Sub thread is caused to execute the variation of logic;
4) incoming pointer is obtained in sub thread, changes the value of uplink command parameter, and value variation causes father's thread
Middle code executes the variation of logic.
Further, the step 3), 4) view actual code design needs repeatedly, alternately irregularly carry out.
Further, step 3), 4) in, caused by switching respectively in the order for executing other side's thread before code segment, in advance
First initial value is changeed back parameter command.
Further, before carrying out the control to another thread by change bid value in thread, the current command is checked
Whether parameter is initial value, is the revaluate that initial value just carries out order, otherwise it is assumed that preceding once to the revaluate of order, there are no draw
Play the variation that thread executes flow direction.
Technical concept of the invention are as follows: devise a kind of thread code switching control technology based on command parameter, the party
Method is suitable for the several operation systems such as Windows/Linux/vxWorks and realizes in C/C++ level.
Using the command parameter of built-in pointer form in the input parameter of thread function, father's thread can after starting sub thread
With setting command value, sub thread executes different code segments according to order;Different command parameters also can be set in sub thread
Value executes flow direction to the code segment of father's thread and controls.Its pointer ginseng that function in C/C++ language is substantially really utilized
The property that carrying input and output are had both possessed by number.
Stick to ready-made Mutli-thread Programming Technology relative to existing general procedure person, and this method is different from ready-made more
Thread synchronization programming mechanism;From system resources consumption, it is light weight method that the technology occupancy resource that this method uses is low;With
WINDOWS message mechanism is compared, and this method real-time is good, is delayed low;This method is based on the parameter pointer realization between function, no
It is related to definition outside or global variable, code wrap is relatively good.
Beneficial effects of the present invention are mainly manifested in: the consumption of reduction kernel resources, code wrap is more brief, real-time is good
It is good.
Detailed description of the invention
Fig. 1 is the schematic diagram of the communication means of customized multithreading control.
Specific embodiment
The invention will be further described below in conjunction with the accompanying drawings.
Referring to Fig.1, a kind of communication means of customized multithreading control, includes the following steps:
1) an argument structure object is designed for the entrance function of sub thread, embedded service parameter and two orders refer to
Needle individual event;The former is required for normal service design function, and the latter is designed by the control management between father and son's thread;
Typedef struct
{
Char*downcmd;// downlink control parameter controls sub thread for father's thread
Char*upcmd;// upload control parameter is used for sub-line process control father's thread
Struct*SunWorkPara;
}SUN_THREAD_PARA;
For example, SunWorkPara is that sub thread needed for former business is passed to parameter,
Downcmd/upcmd is the command parameter of character type, they synthesize new sub thread function
Parameter.
2) it defined in father's thread, initialize 2 local variables, address is opened as uplink, down order parameter pointer
Mover thread;
3) change (down order parameter) local variable value in father's thread, value variation causes sub thread execution to be patrolled
The variation collected;
4) incoming pointer is obtained in sub thread, changes the value of uplink command parameter, and value variation causes father's thread
Middle code executes the variation of logic.
The step 3), 4) design of view actual code needs, and repeatedly, alternately can irregularly carry out.
The step 3), 4) in, caused by switching respectively in the order for executing other side's thread before code segment, in advance ginseng
Initial value is changeed back in number order.
By changing before bid value carries out the control to another thread in thread, check the current command parameter whether be
Initial value is the revaluate that initial value just carries out order, otherwise it is assumed that preceding once to the revaluate of order, there are no cause thread to execute
The variation of flow direction.
Example: a method prototype with C Implementation of pseudocode, sub thread is the thread of long-term terminate-and-stay-resident here, wherein
Father's thread code