Background
Multithreading refers to techniques in which multiple threads are executed concurrently, either from software or hardware. The computer with multithreading capability can execute more than one thread at the same time due to the hardware support, thereby improving the overall processing performance. In a multi-threaded program, data sharing among multiple functions is often implemented using global variables. Since the data space is shared, the global variable is also common to all threads. Some existing development platforms or systems, such as JVM and Android, provide interfaces for directly obtaining thread names, the platforms and systems are biased to high-level languages, and some platforms based on older l inux versions cannot deal with the above principle method for directly obtaining the interfaces for thread names, and when an application program containing multiple threads is run by a Linux system, the application program is often crashed. But the printing of l inux only gives one thread identification code, specifically at which thread crashes. The thread identification code is only a number and is temporarily created, so that the method is quite non-intuitive, and only one thread identification code cannot judge which thread has a problem, which is very inconvenient in debugging.
While maintaining private data for each thread, we may think of assigning an array for storing thread data, and use the thread identification code as the index of the array to implement access, but there is a problem that the thread identification code generated by the system cannot be guaranteed to be a small and continuous integer, and when implemented by an array, because other threads can also access the data in its array, this may cause data confusion. At this time we can solve this problem by means of private data of the thread.
Chinese patent (publication number: CN 105160013A; published date: 2015-12-16) discloses a communication message storage method of a data acquisition system, which comprises the following steps: collecting messages by using at least one message collecting device, and sending the collected messages; constructing a memory buffer area, storing the received messages in the memory buffer area for accumulation by adopting a memory buffer technology, and setting an accumulation threshold value; when the accumulated number of the messages exceeds the accumulation threshold value, starting CPU load detection, and when the detection value is lower than a preset lower limit detection threshold value, compressing the messages; after the message compression is finished, simultaneously starting CPU load detection and I/O load detection, and storing the compressed message into a disk in a file form when the detection values are lower than a preset lower limit detection threshold value; when the file is stored in a disk, a global uniqueness generation method is adopted, and the combined information of the file attribute information is used as a file naming standard for quickly searching and extracting messages; in the process of collecting and storing the communication messages of the whole collecting system, the multithreading technology is used for parallel processing, the packet loss rate of the messages in the collecting process is reduced, and the completeness of the files is ensured when the multithreading technology is used for storing the files.
The communication packet storage method disclosed in the above patent document is not specially designed for multithreading of the Linux system, and cannot correspond the thread identification code to the name thereof, and call out and display the name of the crashed thread in time when the application program crashes.
Disclosure of Invention
Aiming at the problems in the prior art, the invention specially provides a debugging method for obtaining a thread name by utilizing thread private data aiming at a Linux system, and the technical problems to be solved by the invention are as follows: how to enable a Linux system programmer to conveniently and quickly locate a problematic thread so as to improve the efficiency of debugging codes.
The purpose of the invention can be realized by the following technical scheme:
a debugging method for obtaining a thread name by utilizing thread private data is characterized by comprising the following steps:
s1, storage data: after the thread is started, creating private data of the thread, and setting the thread identification code and the corresponding name into a shared data area;
s2, query name: after all threads are started, printing global variables of the threads from the shared data area into a log; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information.
The working principle is as follows: the data of the thread is generally shared in the process, that is, all threads in the process can see the data, and the private data of the thread can only be seen by the thread. Linux provides specific initialization, sets and queries private data of interface operation threads; the method comprises the steps of storing a name corresponding to a current thread identification code by utilizing private data of a thread, inquiring by utilizing the private data of the thread and a shared data area, and printing the identification code of the thread and the name of the thread which are recorded currently when equipment is started or the thread identification code is changed to be used as a log record, so that inquiry when a problem occurs is facilitated. The invention well solves the problem that the thread identification code and the thread name are accurately corresponding by recording the thread name by utilizing the private data of the thread, obtains the name of the crash thread when the application program crashes, is convenient for a programmer to position and debug, and improves the code debugging efficiency. The technical scheme utilizes the characteristic of 'private' in the thread private data, cannot influence other code processes in the thread, cannot excessively increase the code quantity and name conflict, and has better transportability, thereby facilitating the debugging of programmers.
In the debugging method for obtaining a thread name by using thread private data, the step S2 is followed by: step S3, data elimination: when the thread exits, the thread private data release function is called to release the private data of the thread, and meanwhile, the thread information in the shared data area is cancelled. Therefore, the occupied memory of the shared data area can be released in time.
In the debugging method for obtaining the thread name by using the thread private data, the shared data area is refreshed after the step S3. And after the shared data area is updated in time, the correctness of the global information is ensured.
In the above debugging method using thread private data to obtain the thread name, in step S1, the thread identification code and the corresponding name are generated into an ASCII code file, and are stored in the shared data area by using a memory buffer technique.
In the above debugging method using thread private data to obtain a thread name, in step S1, each time a thread is started, it is first determined whether the thread name already exists in the shared data area, and if so, the identifier corresponding to the thread name is updated; if not, the thread name and the thread identification code are simultaneously stored in the shared data area. And inquiring the name corresponding to the single thread according to the thread identification code through the thread private data inquiry interface, and modifying or checking the name corresponding to the thread identification code in the global variable.
In the debugging method for obtaining the thread name by using the thread private data, after the step S1 is completed, the name corresponding to the thread identification code is set as the thread private data. The later proofreading is convenient.
In the above debugging method for obtaining thread names by using thread private data, in step S2, the name corresponding to a single thread is queried according to the thread identification code through the thread private data query interface, and the name corresponding to the thread identification code in the global variable is modified and checked at the same time.
The invention also provides a system for acquiring a thread name by using the thread private data, which is characterized by comprising the following steps:
the creating module is used for creating the private data of the thread after the thread is started;
the memory module is provided with a shared data area and is used for storing the thread identification code and the corresponding name thereof after the thread is started;
the reading module prints the global variables of the threads from the shared data area into the log after all the threads are started; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information.
In the above system for obtaining a thread name by using thread private data, the system further includes:
and the data deleting module is used for canceling the thread information in the shared data area when the thread exits, calling the thread private data releasing function to release the private data of the thread when the thread exits, and canceling the thread information in the shared data area at the same time.
In the above system for obtaining a thread name by using thread private data, the storage space of the storage module is a stack.
Compared with the prior art, the invention has the following advantages:
1. the method and the device well solve the problem that the thread identification code and the thread name cannot be quickly and accurately corresponding by recording the thread name by using the private data of the thread, and can conveniently, quickly, efficiently and accurately acquire the name of the collapsed thread when the application program collapses, so that the problems of positioning and debugging by a programmer are facilitated, and the efficiency of debugging codes is improved.
2. The invention utilizes the characteristic of 'private' in the thread private data, cannot cause influence on other code processes in the thread, cannot excessively increase the code quantity and conflict naming, and has better transportability, thereby facilitating the debugging of programmers.
Detailed Description
The following are specific embodiments of the present invention and are further described with reference to the drawings, but the present invention is not limited to these embodiments.
Example one
As shown in fig. 1, the debugging method for obtaining a thread name by using thread private data in this embodiment includes the following steps:
s1, storage data: after the thread is started, creating private data of the thread, and setting the thread identification code and the corresponding name into a shared data area;
s2, query name: after all threads are started, printing global variables of the threads from the shared data area into a log; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information.
The data of the thread is generally shared in the process, that is, all threads in the process can see the data, and the private data of the thread can only be seen by the thread. Linux provides specific initialization, sets and queries private data of interface operation threads; the method comprises the steps of storing a name corresponding to a current thread identification code by utilizing private data of a thread, inquiring by utilizing the private data of the thread and a shared data area, and printing the identification code of the thread and the name of the thread which are recorded currently when equipment is started or the thread identification code is changed to be used as a log record, so that inquiry when a problem occurs is facilitated. The private data of the thread is used for recording the name of the thread, and the name of the crash thread is obtained when the application program crashes, so that the problems of positioning and debugging by programmers are facilitated, and the efficiency of debugging codes is improved. The scheme utilizes the characteristic of 'private' in the thread private data, cannot influence other code processes in the thread, cannot excessively increase code quantity and name conflict, and has better transportability, thereby facilitating the debugging of programmers.
Example two
The embodiment is further optimized on the basis of the first embodiment, and as shown in fig. 2, the debugging method for obtaining the thread name by using the thread private data in the embodiment includes the following steps:
s1, storage data: after the thread is started, creating private data of the thread, and setting the thread identification code and the corresponding name into a shared data area;
s2, query name: after all threads are started, printing global variables of the threads from the shared data area into a log; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information.
S3, data elimination: when the thread exits, the thread private data release function is called to release the private data of the thread, and meanwhile, the thread information in the shared data area is cancelled. Therefore, the occupied memory of the shared data area can be released in time.
After initialization, setting and inquiring private data of an interface operation thread; the method comprises the steps of storing a name corresponding to a current thread identification code by utilizing private data of a thread, inquiring by utilizing the private data of the thread and a shared data area, and printing the identification code of the thread and the name of the thread which are recorded currently when equipment is started or the thread identification code is changed to be used as a log record, so that inquiry when a problem occurs is facilitated. The private data of the thread is used for recording the name of the thread, and the name of the crash thread is obtained when the application program crashes, so that the problems of positioning and debugging by programmers are facilitated, and the efficiency of debugging codes is improved. The scheme utilizes the characteristic of 'private' in the thread private data, cannot influence other code processes in the thread, cannot excessively increase code quantity and name conflict, and has better transportability, thereby facilitating debugging of programmers; the occupied memory for releasing the shared data area can be reduced by eliminating the data in time.
EXAMPLE III
The embodiment is further optimized and perfected on the basis of the second embodiment, and the debugging method for obtaining the thread name by using the thread private data in the embodiment comprises the following steps:
s1, storage data: after the thread is started, creating private data of the thread, and setting the thread identification code and the corresponding name into a shared data area; in the step, the thread identification code and the corresponding name are generated into an ASCII code file, and the ASCII code file is stored in a shared data area by adopting a memory buffer technology;
s2, query name: after all threads are started, printing global variables of the threads from the shared data area into a log; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information; inquiring the name corresponding to the single thread according to the thread identification code through a thread private data inquiry interface, and modifying and checking the name corresponding to the thread identification code in the global variable;
s3, data elimination: when the thread exits, calling the thread private data release function, releasing the private data of the thread, simultaneously canceling the thread information in the shared data area, and refreshing the shared data area; therefore, the occupied memory of the shared data area can be released in time, and the correctness of the global information is ensured.
After the whole system is operated, new threads are started and quitted continuously, data are stored and analyzed according to the steps S1, S2 and S3 in sequence, in the whole circulation process, when the threads are started, whether the thread name exists in a shared data area is judged firstly, and if the thread name exists, the identification code corresponding to the thread name is updated; if the thread name and the thread identification code do not exist, the thread name and the thread identification code are simultaneously stored in the shared data area, the name corresponding to a single thread is inquired through the thread private data inquiry interface according to the thread identification code, and meanwhile, the name corresponding to the thread identification code in the global variable is modified or checked.
After initialization, setting and inquiring private data of an interface operation thread; the method comprises the steps of storing a name corresponding to a current thread identification code by utilizing private data of a thread, inquiring by utilizing the private data of the thread and a shared data area, and printing the identification code of the thread and the name of the thread which are recorded currently when equipment is started or the thread identification code is changed to be used as a log record, so that inquiry when a problem occurs is facilitated. The private data of the thread is used for recording the name of the thread, and the name of the crash thread is obtained when the application program crashes, so that the problems of positioning and debugging by programmers are facilitated, and the efficiency of debugging codes is improved. The scheme utilizes the characteristic of 'private' in the thread private data, cannot influence other code processes in the thread, cannot excessively increase the code quantity and name conflict, and has better transportability.
Example four
The present embodiment provides a system for obtaining a thread name by using thread private data, including:
the creating module is used for creating the private data of the thread after the thread is started;
the memory module is provided with a shared data area and is used for storing the thread identification code and the corresponding name thereof after the thread is started;
and the reading module is used for reading the name corresponding to the thread from the shared data area when the thread crashes.
After the thread is started, the creation module creates the private data of the thread, the storage module forms a shared data area, the thread identification code and the corresponding name are set in the shared data area in the storage module, and after all threads are started, the global variable of the thread is printed from the shared data area to the log; when the program crashes, the reading module queries the thread identification code and the printed log in the crash information, finds the corresponding name and displays the name, so that a programmer can conveniently locate and debug the system problems, the code debugging efficiency is improved, the influence on other code processes in the thread is avoided, the code quantity and the name conflict are not excessively increased, and the portability is good.
EXAMPLE five
As shown in fig. 3, the present embodiment is further improved on the basis of the fourth embodiment, in which the system for obtaining a thread name by using thread private data includes:
the creating module is used for creating the private data of the thread after the thread is started;
the memory module is provided with a shared data area and is used for storing the thread identification code and the corresponding name thereof after the thread is started;
and the reading module is used for reading the name corresponding to the thread from the shared data area when the thread crashes.
And the data deleting module is used for canceling the thread information in the shared data area when the thread exits.
In this embodiment, the storage space of the storage module is a stack. After the thread is started, the creation module creates the private data of the thread, the storage module forms a shared data area, the thread identification code and the corresponding name are set in the shared data area in the storage module, and after all threads are started, the global variable of the thread is printed from the shared data area to the log; when the program crashes, the thread identification code and the printed log in the crash information are inquired through the data analysis module and the reading module, and the corresponding name is found and displayed. When the thread exits, the thread private data release function is called to release the private data of the thread, and meanwhile, the thread information in the shared data area is cancelled through the data deletion module, so that the memory occupied by the shared data area is released in time.
EXAMPLE six
The embodiment is further optimized and perfected on the basis of the fifth embodiment, and the system for acquiring the thread name by using the thread private data in the embodiment includes:
the creating module is used for creating the private data of the thread after the thread is started;
the memory module is provided with a shared data area and is used for storing the thread identification code and the corresponding name thereof after the thread is started; generating an ASCII code file by the started thread identification code and the corresponding name information, and storing the ASCII code file in a shared data area of a storage module by adopting a memory buffer technology;
the reading module is used for reading the name corresponding to the thread from the shared data area when the thread crashes; after all threads are started, printing global variables of the threads from the shared data area into a log; when the program crashes, the corresponding name is found and displayed by inquiring the thread identification code and the printed log in the crash information;
the data deleting module is used for canceling the thread information in the shared data area when the thread exits; when the thread exits, calling the thread private data release function, releasing the private data of the thread, simultaneously canceling the thread information in the shared data area, and refreshing the shared data area; the occupied memory of the shared data area is released in time;
and the data checking module is used for modifying and checking the name corresponding to the thread identification code in the global variable to ensure the correctness of the global information.
After the whole system is operated, new threads are started and quitted continuously, data are stored and analyzed according to the steps S1, S2 and S3 in sequence, in the whole circulation process, when the threads are started, whether the thread name exists in a shared data area is judged firstly, and if the thread name exists, the identification code corresponding to the thread name is updated; if the thread name and the thread identification code do not exist, the thread name and the thread identification code are simultaneously stored in the shared data area, and the name corresponding to the single thread is inquired through the thread private data inquiry interface and the thread identification code.
The specific embodiments described herein are merely illustrative of the spirit of the invention. Various modifications or additions may be made to the described embodiments or alternatives may be employed by those skilled in the art without departing from the spirit or ambit of the invention as defined in the appended claims.