I. Overview
In the actual software program, because of the large amount of code, the call relationship between functions is more complex, so the operation of some global variables should be extra careful. In the program, the mutex is generally used to lock the way to ensure that the operation of the global variable uniqueness.
In this paper, the implementation of C code for the mutex locking and unlocking operation in Linux is introduced in detail, which provides a useful reference for the development of related software.
second, locking and unlocking function and time structure body introduction
1. Add Lock function Pthread_mutex_timedlock
function prototypes: int pthread_mutex_timedlock (pthread_mutex_t *restrict Mutex, const struct TIMESPEC *restrict abstime);
Function Description: The Pthread_mutex_timedlock function is used to lock the mutex represented by the mutex, and if the mutex is locked, the function waits until the mutex is unlocked and the wait time is abstime the specified amount.
function return value: Return 0 means lock is successful, other indicates lock failure.
2. Unlocking function Pthread_mutex_unlock
Function prototype: int pthread_mutex_unlock (pthread_mutex_t *mutex);
Function Description: The Pthread_mutex_unlock function is used to release the mutex represented by the mutex.
function return value: Return 0 means lock is successful, other indicates lock failure.
3.timespec Structural Body
struct timespec{ time_t tv_sec; /*second*/ long tv_nsec; /*nanosecond*/}
The struct has two member variables: tv_sec represents seconds, and tv_nsec represents nanoseconds.
4.timeval Structural Body
struct timeval{ time_t tv_sec; /*seconds*/ suseconds tv_usec; /*microseconds*/}
The struct has two member variables: tv_sec represents seconds, and tv_usec represents microseconds.
Third, C program implementation
This program is named "Lockandunlock.c", where "Mutexlock" is the lock function, "Mutexunlock" is the unlocking function.
The specific code is as follows:
/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: lockandunlock.c* File ID: None* Content Summary: Demonstrates the invocation of locking and unlocking functions* Other instructions: None* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20150509***********************************************************************/#include <sys/time.h>#include <pthread.h>Macro definition#define LOCKTIMEOUT 5000//Mutex timeout time 5000 msGlobal variable Pthread_mutex_T g_mutex;//redefine data type typedef signed INT int32;//function declaration INT32 Mutexlock (); INT32 mutexunlock (); INT32 main ();*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20150509 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {INT32 iretcode = 0;iRetCode = Mutexlock (); Mutex plus lockif (iRetCode < 0) {printf ("Exec mutexlock failed!\n");return-1; }printf ("--------------\ n");printf ("ADD Code here!\n");printf ("--------------\ n");iRetCode = Mutexunlock (); Mutex Unlockingif (iRetCode < 0) {printf ("Exec mutexunlock failed!\n");return-1; }return 0;}/*********************************************************************** Function Description: Mutex plus lock* Input parameters: None* Output parameters: None* Return value: 0-Success-1-Failed* Other instructions: None* Modified Date version number modify the content of the person* ------------------------------------------------------------------* 20150509 V1.0 Zhou Zhaoxiong created********************************************************************/INT32 Mutexlock () {struct Timeval tcurrenttime;struct Timespec ttimeout;INT32 iretcode = 0;gettimeofday (&tcurrenttime, NULL); Gets the current absolute timettimeout.tv_sec = tcurrenttime.tv_sec + locktimeout/1000; Specify the time-out periodttimeout.tv_nsec = tcurrenttime.tv_usec *;iRetCode = Pthread_mutex_timedlock (&g_mutex, &ttimeout);if (iRetCode! = 0) {printf ("Mutexlock:exec Pthread_mutex_timedlock failed, retcode=%d\n", iRetCode);return-1; }return 0;}/*********************************************************************** Function Description: Mutex unlocking* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ------------------------------------------------------------------* 20150509 V1.0 Zhou Zhaoxiong created********************************************************************/int32 Mutexunlock () {INT32 iretcode = 0;iRetCode = Pthread_mutex_unlock (&g_mutex);if (iRetCode! = 0) {printf ("Mutexunlock:exec Pthread_mutex_unlock failed, retcode=%d\n", iRetCode);return-1; }return 0;}
Iv. Compilation of files and results of Operation
Execute "gcc-g-pthread-o lockandunlock lockandunlock.c" or "gcc lockandunlock.c-o lockandunlock-lpthread" command under Linux to generate " Lockandunlock ". Then execute the "lockandunlock" command, the results of the program run as follows:
--------------Add code here!--------------
v. Summary
This paper gives the implementation of C code for the locking and unlocking operation of the mutex in Linux. The "Mutexlock" and "Mutexunlock" functions in the program can be used as APIs for other program calls that require similar operations.
My public number: ZHOUZXI, please scan the following two-dimensional code:
Implementation of C code for mutex locking and unlocking operation under Linux