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

Signal-based inter-thread communication method Download PDF

Info

Publication number
CN115658349B
CN115658349B CN202211461010.6A CN202211461010A CN115658349B CN 115658349 B CN115658349 B CN 115658349B CN 202211461010 A CN202211461010 A CN 202211461010A CN 115658349 B CN115658349 B CN 115658349B
Authority
CN
China
Prior art keywords
thread
data
signal
target
target thread
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.)
Active
Application number
CN202211461010.6A
Other languages
Chinese (zh)
Other versions
CN115658349A (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 the target thread continuously inquires the data storage structure of the data source of the target thread and returns new transfer data by calling an inquiry 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 a certain complexity in implementation, and needs to create a communication management center, and meanwhile, the use of a thread mapping relationship and a pointer in a mixed manner can reduce program robustness and cause confusion to users.
The invention discloses an invention patent with publication number CN 109697131A, namely an inter-process parameter management and synchronization method and device, and relates to an inter-process parameter management and synchronization method and device, but the method is not suitable for inter-thread communication. The invention patent with publication number CN1273910C, namely the bus type interprocess communication method, has the same problem and is not suitable for interprocess 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 struct list _ head comprises two pointers prev and next pointing to a list _ head structure, 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.
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 ttreamqueue of each said element msghist 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; }; wherein, the signal variable is used for storing the signal of msgQueue, and the pMsgData variable is used for storing the 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.
Furthermore, the query function is a GetThreadSignal function, the function is added with 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 provided by the invention 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 the threads is realized by data transmission through an msgList in the tThreadInfo, wherein the msgList is a queue, meets the first-in first-out principle, and is realized by adopting a circular array, as shown in FIG. 2.
Wherein front always points to the head of the queue, and real always points to the tail of the queue, and the values of front and real 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, then in the code of thread a, sendSignalToThread function is called, the data to be transferred and the name of the target thread are taken as function parameters, and the sendSignalToThread is called, and sendSignalToThread finds the tthreadfo structure of the thread from the thread management linked list according to the name of the target thread (thread B), and inserts the data transferred by thread a into msglst in tthreadfo.
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 reference of the function structure is 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.
The method adopts pure C coding, can directly communicate among threads by sending signals, can carry custom data in the signals, has strong flexibility and applicability, and can be used in C/C + + projects. 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 thereto. Various changes in form and detail may be made therein without departing from the spirit and scope of the invention as defined by the appended claims.

Claims (4)

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;
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 threedQueueMutex; };
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 struct list _ head comprises two pointers prev and next pointing to a list _ head structure, 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 the msgl against the lock of the msgl;
the data structure tThreadQueue of the 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; }; wherein, the signal variable is used for storing the signal of msgQueue, and the pMsgData variable is used for storing the 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;
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 msgList is a queue, meets the first-in first-out principle, and is realized by adopting a cyclic array.
3. A signal-based inter-thread communication method according to claim 2, 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.
4. A signal based inter-thread communication method according to claim 3, 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 CN115658349A (en) 2023-01-31
CN115658349B true 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)

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7213244B2 (en) * 2003-03-13 2007-05-01 International Business Machines Corporation Apparatus and method for distribution of work on a doubly linked list among processing threads
CN102867018B (en) * 2012-07-27 2015-04-22 北大方正集团有限公司 Method for analogue signal communication between threads in database system
CN103345422B (en) * 2013-07-02 2019-01-29 厦门雅迅网络股份有限公司 A kind of 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
CN115658349A (en) 2023-01-31

Similar Documents

Publication Publication Date Title
US7246182B2 (en) Non-blocking concurrent queues with direct node access by threads
US5835906A (en) Methods and apparatus for sharing stored data objects in a computer system
US6898607B2 (en) Proposed syntax for a synchronized commands execution
US5832508A (en) Method for deallocating a log in database systems
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
US6665675B1 (en) Shared file system having a token-ring style protocol for managing meta-data
US7783601B2 (en) Replicating and sharing data between heterogeneous data systems
WO2018001135A1 (en) Method for processing database transaction, client and server
EP0766185A2 (en) Methods and apparatus for providing transparent persistent data support to foreign data types
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
JPH0962570A (en) Method and device for managing database
Cheriton UIO: A Uniform I/O system interface for distributed systems
EP3238421A1 (en) System for high-throughput handling of transactions in data-partitioned, distributed, relational database management system
CN108491332A (en) A kind of real-time buffering updating method and system based on Redis
JP2009540466A (en) Application program interface for managing media files
KR100313844B1 (en) Locking tool data objects in a framework environment
CN113342507A (en) Distributed lock service implementation method and device and computer equipment
CN115658349B (en) Signal-based inter-thread communication method
US7831642B1 (en) Page cache management for a shared file
US20030200457A1 (en) Enhancement to the MCS lock for increased functionality and improved programmability
EP0889396A1 (en) Thread synchronisation via selective object locking
WO2003042822A1 (en) Method and apparatus for facilitating compact object headers
US20110302375A1 (en) Multi-Part Aggregated Variable in Structured External Storage
US11843663B1 (en) Vector-scalar logical clock and associated method, apparatus and system

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