CN115658349A - Signal-based inter-thread communication method - Google Patents

Signal-based inter-thread communication method Download PDF

Info

Publication number
CN115658349A
CN115658349A CN202211461010.6A CN202211461010A CN115658349A CN 115658349 A CN115658349 A CN 115658349A CN 202211461010 A CN202211461010 A CN 202211461010A CN 115658349 A CN115658349 A CN 115658349A
Authority
CN
China
Prior art keywords
thread
data
signal
target
function
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202211461010.6A
Other languages
Chinese (zh)
Other versions
CN115658349B (en
Inventor
宋强
刘爽
闵济海
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Nanjing Tetra Electronic Technology Co ltd
Original Assignee
Nanjing Tetra Electronic Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Nanjing Tetra Electronic Technology Co ltd filed Critical Nanjing Tetra Electronic Technology Co ltd
Priority to CN202211461010.6A priority Critical patent/CN115658349B/en
Publication of CN115658349A publication Critical patent/CN115658349A/en
Application granted granted Critical
Publication of CN115658349B publication Critical patent/CN115658349B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention discloses a signal-based inter-thread communication method, which comprises the following steps: creating a bidirectional circular linked list; starting a thread, and inserting a data structure of thread information into a data domain of a corresponding thread node in the bidirectional circular linked list; acquiring a data sending request from any initiating thread to another target thread in a bidirectional circular linked list; inquiring a data lock structure of a data domain in the target thread, if the data lock structure is in a non-locking state, adjusting the data lock structure of the target thread to be in a locking state, calling a sending function to send data to be transmitted to the target thread and storing the data to a data storage structure of the data domain in the target thread; and continuously querying the data storage structure of the data source of the target thread by the target thread, and returning new transfer data by calling a query function. The threads of the invention are communicated by sending signals directly, the signals can carry user-defined data, and the flexibility and the applicability are strong.

Description

Signal-based inter-thread communication method
Technical Field
The invention relates to the technical field of system inter-thread communication, in particular to a signal-based inter-thread communication method.
Background
At present, there are many interprocess communication methods in the linux system, including pipelines, signals, sockets, shared memories, etc., and the communication means among threads is mainly used for thread synchronization, including mutual exclusion locks, read-write locks, conditional variables, spin locks, semaphore mechanisms, etc.
Thread communication has no communication mechanism for data exchange like in process communication. Simple inter-thread data exchange may use locked global variables, but excessive use of global variables can disrupt the structure of the program, making code difficult to maintain.
CN 102867018A proposes a method for communication of analog signals between threads in a database system, which has certain complexity in implementation, and needs to create a communication management center, and meanwhile, the use of a mapping relationship of threads and a pointer in a mixed manner may reduce program robustness, and cause confusion to users.
The invention patent with publication number CN 109697131A, "parameter management between processes, synchronization method and apparatus", relates to a parameter management between processes, synchronization method and apparatus, but is not suitable for communication between threads. The invention patent "bus type inter-process communication method" with publication number CN1273910C also has the same problem, and is not suitable for inter-thread communication.
Disclosure of Invention
The purpose of the invention is as follows: the invention aims to provide a signal-based inter-thread communication method aiming at the defects of the prior art, the inter-thread can be directly communicated by sending signals, the signals can carry custom data, and the flexibility and the applicability are strong.
The technical scheme is as follows: the invention discloses a signal-based inter-thread communication method, which comprises the following steps:
s1: creating a bidirectional circular linked list;
s2: starting a thread, and inserting a data structure of thread information into a data domain of a corresponding thread node in the bidirectional circular linked list;
s3: the thread of the sender calls a signal sending function to request to send data to a target thread, and when the signal sending function is called, the thread of the sender takes the name of the target thread and the data to be transmitted as function parameters;
s4: after receiving the request, the signal sending function acquires a data domain corresponding to the target thread node from the bidirectional circular linked list according to the name of the target thread in the request, and then stores the data to be transmitted to a data storage structure of the data domain in the target thread;
s5: the target thread continuously inquires the data storage structure of the data domain of the target thread and returns new transmitted data by calling an inquiry function.
Further perfecting the technical scheme, the data structure of the thread information is as follows: struct tThreadInfo { struct list _ head list; pthread _ t pid; char threadName [32]; tThreadQueue msgList; pthread _ mutex _ t threequeumutex; };
wherein, struct tThreadInfo represents that the thread information tThreadInfo is a structure type, and each structure of the thread information tThreadInfo comprises the following elements: the data type structure _ head comprises two pointers prev and next pointing to a structure of the structure _ head, and the element list is used for traversing a bidirectional circular linked list; the data type pthread _ t is a declaration thread ID, and the element pid is used for storing the thread ID; the data type char is a character string, the element threadName [32] is used for storing the thread name, and the digit of the character string is 32 bits; the data type tThreadQueue is a self-defined structure body, and the element msgList is used for storing data information transmitted among threads; the data type pthread _ mutex _ t is a thread lock, and the element threequeumutex protects the access of msgList against the msgList lock.
Furthermore, the msgList is a queue, meets the first-in first-out principle, and is realized by adopting a cyclic array.
Further, the data structure tThreadQueue of each said element msgList is defined as: struct tThreadQueue { tThreadMsg msgQueue [ N ]; int front; int real; };
wherein: the msgQueue [ N ] stores real interactive information, including signals and carried data, N is the number of data stored in a single thread, and the data structure of each piece of interactive information tThreadMsg is as follows: struct tThreadMsg { int signal; void pMsgData; }; the signal variable is used for storing a signal of msgQueue, and the pMsgData variable is used for storing data in msgQueue;
int front is a head pointer always pointing to the head of the queue, and int rear is a tail pointer always pointing to the tail of the queue.
The signal is used for telling the target thread which function the transmitted message needs to execute, and the target thread can execute different logic processing according to different signals. For example, the target thread supports functions such as taking a picture, recording a sound, recording a video, etc., then the target thread is informed of which function to execute by different signals.
Further, the sending function is a sendSignalToThread function, data to be transmitted and a target thread name are taken as functions to be entered, sendSignalToThread finds a tthreadfo structure of the target thread from the bidirectional circular linked list according to the target thread name, and inserts the data to be transmitted by the sender thread into an msglst in the tthreadfo.
Further, the query function is a GetThreadSignal function, the function is referred to as the name of the target thread, the target thread queries the msgl structure in tThreadInfo of the target thread through the GetThreadSignal function, and the msgl is taken out and returned as a return value.
Has the advantages that: compared with the prior art, the invention has the advantages that: the pure C coding is adopted, the threads can communicate directly by sending signals, the signals can carry custom data, the flexibility and the applicability are strong, and the method can be used in a C/C + + project. The code of the patent is realized in a Linux system, and can run under Windows with slight modification.
Drawings
FIG. 1 is a schematic diagram of thread management in the present invention;
fig. 2 is a schematic diagram of a message queue in the present invention.
Detailed Description
The technical solution of the present invention is described in detail below with reference to the accompanying drawings, but the scope of the present invention is not limited to the embodiments.
The signal-based inter-thread communication method comprises the management of threads and the data interaction among the threads.
As shown in fig. 1, a bidirectional circular linked list (list.h) in a linux kernel is used to manage a new thread, and each time a new thread is started, a data structure of thread information is inserted into the linked list, and the data structure of the thread information is as follows:
typedef struct tThreadInfo
{
struct list_head list;
pthread_t pid;
char threadName[32];
tThreadQueue msgList;
pthread_mutex_t threadQueueMutex;
}tThreadInfo;
each thread has a corresponding tThreadInfo structure data, wherein a list _ head stores a next pointer and a prev pointer for traversing a linked list, a pid stores a thread id, a headName stores a thread name, an msgList stores data information transmitted between threads and contains transmitted signals and data, and the thredQueueMutex is a lock aiming at the msgList and protects the access of the msgList.
the threadQueueMutex is the exclusive lock of the thread and is provided by a standard function library, a pthread _ mutex _ lock function is called when the lock is added, msgList data can be safely accessed, other threads cannot access the msgList data, and when the access is completed, a pthread _ mutex _ unlock function is called to release the lock, and other threads can access the lock. pthread _ mutex _ lock and pthread _ mutex _ unlock are both provided by a library of standard functions.
(2) Data interaction of threads is realized by transmitting data through an msgList in tThreadInfo, the msgList is a queue, the first-in first-out principle is met, and the data interaction is realized by adopting a circular array, as shown in FIG. 2.
The front always points to the head of the queue, the rear always points to the tail of the queue, and the values of the front and the rear are changed correspondingly whenever an element is inserted or deleted, and the msgList structure of each thread is defined as follows:
typedef struct tThreadQueue
{
tThreadMsg msgQueue[100];
int front;
int rear;
}tThreadQueue;
typedef struct tThreadMsg
{
int signal;
void *pMsgData;
}tThreadMsg;
each thread can store 100 data information, and after one piece of data is processed, the data information needs to be deleted in time to release queue space, wherein the msgQueue stores real interactive information including signals and carried data.
The signal type is int, digital type, the meaning represented by int is defined when the target thread is created, for example, the target thread supports functions of photographing, recording and the like, so that 1 can be used for photographing, 2 can be used for recording, and 3 can be used for recording, if a certain thread wants to make a photograph of the target thread, the value of signal is filled in 1, the pMsgData can fill in the directory for storing the photograph, so that when the target thread receives the signal 1, the target thread indicates that the photographing function of the target thread needs to be called, and the photograph is stored in the path filled in the pMsgData. If the value of the signal sent to the target thread is 4, the target thread finds itself does not support the function of the signal when receiving the signal 4, and ignores the signal.
If thread a needs to send data to thread B at a certain time, in the code of thread a, call sendSignalToThread function, and take the data to be transferred and the name of the target thread as function parameters, and call, sendSignalToThread will find the ttreaminfo structure of the thread from the thread management linked list according to the name of the target thread (thread B), and insert the data transferred by thread a into the msglst in ttreaminfo.
Meanwhile, the thread B needs to continuously inquire the msgList structure in the tThreadInfo of the thread B to see whether new data comes in or not, the function is realized by calling a GetThreadSignal function, one parameter of the function structure is called, namely the name of the thread B, then the tThreadInfo structure of the thread B is searched from the thread management linked list, and the msgList is taken out and returned as a return value.
This patent adopts pure C to encode, and the thread can directly communicate through sending the signal, can carry the custom data in the signal, and flexibility and suitability are very strong, can use in the C/C + + project. The code of the patent is realized in a Linux system, and can run under Windows with slight modification.
As noted above, while the present invention has been shown and described with reference to certain preferred embodiments, it is not to be construed as limited to the invention itself. Various changes in form and details may be made therein without departing from the spirit and scope of the invention as defined by the appended claims.

Claims (6)

1. A method for signal-based inter-thread communication, comprising the steps of:
s1: creating a bidirectional circular linked list;
s2: starting a thread, and inserting a data structure of thread information into a data domain of a corresponding thread node in the bidirectional circular linked list;
s3: the thread of the sender calls a signal sending function to request to send data to a target thread, and when the signal sending function is called, the thread of the sender takes the name of the target thread and the data to be transmitted as function parameters;
s4: after receiving the request, the signal sending function acquires a data domain corresponding to the target thread node from the bidirectional circular linked list according to the name of the target thread in the request, and then stores the data to be transmitted to a data storage structure of the data domain in the target thread;
s5: the target thread continuously inquires the data storage structure of the data domain of the target thread and returns new transmitted data by calling an inquiry function.
2. The signal-based inter-thread communication method of claim 1, wherein: the data structure of the thread information is as follows: struct tThreadInfo { struct list _ head list; pthread _ t pid; char threadName [32]; tThreadQueue msgList; pthread _ mutex _ t threequeumutex; };
wherein, struct tThreadInfo represents that the thread information tThreadInfo is a structure type, and each structure of the thread information tThreadInfo comprises the following elements: the data type structure _ head comprises two pointers prev and next pointing to a structure of the structure _ head, and the element list is used for traversing a bidirectional circular linked list; the data type pthread _ t is a declaration thread ID, and the element pid is used for storing a thread ID; the data type char is a character string, the element threadName [32] is used for storing the thread name, and the digit of the character string is 32 bits; the data type tThreadQueue is a self-defined structure body, and the element msgList is used for storing data information transmitted among threads; the data type pthread _ mutex _ t is a thread lock, and the element threequeumutex protects the access of msgList against the msgList lock.
3. The signal-based inter-thread communication method of claim 2, wherein: the msgList is a queue, meets the first-in first-out principle, and is realized by adopting a cyclic array.
4. A signal based inter-thread communication method according to claim 3, wherein: the data structure ttthreadqueue of the element msgl is defined as: struct tThreadQueue { tThreadMsg msgQueue [ N ]; int front; int real; };
wherein: the msgQueue [ N ] stores real interactive information comprising signals and carried data, N is the number of data stored in a single thread, and each piece of interactive information tThreadMsg has a data structure as follows: struct tThreadMsg { int signal; void pMsgData; }; the signal variable is used for storing a signal of msgQueue, and the pMsgData variable is used for storing data in msgQueue;
int front is a head pointer always pointing to the head of the queue, and int rear is a tail pointer always pointing to the tail of the queue.
5. The signal-based inter-thread communication method of claim 4, wherein: the sending function is a sendSignalToThread function, data to be transmitted and the name of a target thread are taken as functions to be referred, sendSignalToThread finds a tthreadfo structure of the target thread from a bidirectional circular linked list according to the name of the target thread, and the data to be transmitted by a sender thread is inserted into msgl in the tthreadfo.
6. The signal-based inter-thread communication method of claim 5, wherein: the query function is a GetThreadSignal function, the function is referred to as the name of the target thread, the target thread queries the msgList structure in the tThreadInfo of the target thread through the GetThreadSignal function, and the msgList is taken out and returned as a return value.
CN202211461010.6A 2022-11-17 2022-11-17 Signal-based inter-thread communication method Active CN115658349B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211461010.6A CN115658349B (en) 2022-11-17 2022-11-17 Signal-based inter-thread communication method

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211461010.6A CN115658349B (en) 2022-11-17 2022-11-17 Signal-based inter-thread communication method

Publications (2)

Publication Number Publication Date
CN115658349A true CN115658349A (en) 2023-01-31
CN115658349B CN115658349B (en) 2023-03-14

Family

ID=85019357

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211461010.6A Active CN115658349B (en) 2022-11-17 2022-11-17 Signal-based inter-thread communication method

Country Status (1)

Country Link
CN (1) CN115658349B (en)

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040181504A1 (en) * 2003-03-13 2004-09-16 International Business Machines Corporation Apparatus and method for distribution of work on a doubly linked list among processing threads
CN102867018A (en) * 2012-07-27 2013-01-09 上海方正数字出版技术有限公司 Method for analogue signal communication between threads in database system
CN103345422A (en) * 2013-07-02 2013-10-09 厦门雅迅网络股份有限公司 Multithreading hard real-time control method based on Linux
CN108595282A (en) * 2018-05-02 2018-09-28 广州市巨硅信息科技有限公司 A kind of implementation method of high concurrent message queue

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040181504A1 (en) * 2003-03-13 2004-09-16 International Business Machines Corporation Apparatus and method for distribution of work on a doubly linked list among processing threads
CN102867018A (en) * 2012-07-27 2013-01-09 上海方正数字出版技术有限公司 Method for analogue signal communication between threads in database system
CN103345422A (en) * 2013-07-02 2013-10-09 厦门雅迅网络股份有限公司 Multithreading hard real-time control method based on Linux
CN108595282A (en) * 2018-05-02 2018-09-28 广州市巨硅信息科技有限公司 A kind of implementation method of high concurrent message queue

Also Published As

Publication number Publication date
CN115658349B (en) 2023-03-14

Similar Documents

Publication Publication Date Title
US7246182B2 (en) Non-blocking concurrent queues with direct node access by threads
US6898607B2 (en) Proposed syntax for a synchronized commands execution
US5522077A (en) Object oriented network system for allocating ranges of globally unique object identifiers from a server process to client processes which release unused identifiers
US5832508A (en) Method for deallocating a log in database systems
US6665675B1 (en) Shared file system having a token-ring style protocol for managing meta-data
US8930669B2 (en) Tiered data management method and system for high performance data monitoring
WO2018001135A1 (en) Method for processing database transaction, client and server
JP5194005B2 (en) Application program interface for managing media files
JPH10187527A (en) Device and method for preventing access contention
CA2398148C (en) System and method for managing bi-directional relationships between objects
KR19990029323A (en) Application program interface, how to implement it, and computer program products
WO2000000906A2 (en) Method and apparatus for locking
JP2000155679A (en) Method and device for preparing stable repeater for shared data collection
US20160210313A1 (en) System for high-throughput handling of transactions in a data-partitioned, distributed, relational database management system
CN108491332A (en) A kind of real-time buffering updating method and system based on Redis
US5101488A (en) Method for retrieving and updating data in a real-time data base system
CN115658349B (en) Signal-based inter-thread communication method
CA2201275A1 (en) Locking tool data objects in a framework environment
US7831642B1 (en) Page cache management for a shared file
US20030200457A1 (en) Enhancement to the MCS lock for increased functionality and improved programmability
CN113342507A (en) Distributed lock service implementation method and device and computer equipment
US7464070B2 (en) Database query operations using storage networks
Walker et al. Joinpoints as ordered events: Towards applying implicit context to aspect-orientation
US7028287B2 (en) Method and apparatus for facilitating compact object headers
Tripathi et al. An implementation of the object‐oriented concurrent programming language SINA

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant