openharmony 鸿蒙 capi-mutex-h

2026-08-25 浏览 (1)

mutex.h

Overview

The mutex.h file declares the mutex APIs in C.

File to include: <ffrt/mutex.h>

Library: libffrt.z.so

System capability: SystemCapability.Resourceschedule.Ffrt.Core

Since: 10

Related module: FFRT

Summary

Function

NameDescription
FFRT_C_API int ffrt_mutexattr_init(ffrt_mutexattr_t* attr)Initializes the mutex attribute.
FFRT_C_API int ffrt_mutexattr_settype(ffrt_mutexattr_t* attr, int type)Sets the mutex attribute type.
FFRT_C_API int ffrt_mutexattr_gettype(ffrt_mutexattr_t* attr, int* type)Obtains the mutex type.
FFRT_C_API int ffrt_mutexattr_destroy(ffrt_mutexattr_t* attr)Destroys the mutex attribute. This API needs to be called by users.
FFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr)Initializes a mutex.
FFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex)Locks a mutex.
FFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex)Unlocks a mutex.
FFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex)Attempts to lock a mutex.
FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex)Destroys a mutex.

Function Description

ffrt_mutexattr_init()

FFRT_C_API int ffrt_mutexattr_init(ffrt_mutexattr_t* attr)

Description

Initializes the mutex attribute.

Since: 12

Parameters

NameDescription
ffrt_mutexattr_t* attrPointer to the mutex attribute.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex attribute is initialized;
returns ffrt_error_inval otherwise.

ffrt_mutexattr_settype()

FFRT_C_API int ffrt_mutexattr_settype(ffrt_mutexattr_t* attr, int type)

Description

Sets the mutex attribute type.

Since: 12

Parameters

NameDescription
ffrt_mutexattr_t* attrPointer to the mutex attribute.
int typeMutex type.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex attribute type is set;
returns ffrt_error_inval if the pointer to the mutex attribute is null
or the mutex is not of the ffrt_mutex_normal or ffrt_mutex_recursive type.

ffrt_mutexattr_gettype()

FFRT_C_API int ffrt_mutexattr_gettype(ffrt_mutexattr_t* attr, int* type)

Description

Obtains the mutex type.

Since: 12

Parameters

NameDescription
ffrt_mutexattr_t* attrPointer to the mutex attribute.
int* typePointer to the mutex type.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex type is obtained;
returns ffrt_error_inval if the pointer to the mutex attribute or mutex type is null.

ffrt_mutexattr_destroy()

FFRT_C_API int ffrt_mutexattr_destroy(ffrt_mutexattr_t* attr)

Description

Destroys the mutex attribute. This API needs to be called by users.

Since: 12

Parameters

NameDescription
ffrt_mutexattr_t* attrPointer to the mutex attribute.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex attribute is destroyed;
returns ffrt_error_inval otherwise.

ffrt_mutex_init()

FFRT_C_API int ffrt_mutex_init(ffrt_mutex_t* mutex, const ffrt_mutexattr_t* attr)

Description

Initializes a mutex.

Since: 10

Parameters

NameDescription
ffrt_mutex_t* mutexPointer to the mutex.
const ffrt_mutexattr_t* attrPointer to the mutex attribute.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex is initialized;
returns ffrt_error_inval otherwise.

ffrt_mutex_lock()

FFRT_C_API int ffrt_mutex_lock(ffrt_mutex_t* mutex)

Description

Locks a mutex.

Since: 10

Parameters

NameDescription
ffrt_mutex_t* mutexPointer to the mutex.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex is locked;
returns ffrt_error_inval or blocks the calling thread otherwise.

ffrt_mutex_unlock()

FFRT_C_API int ffrt_mutex_unlock(ffrt_mutex_t* mutex)

Description

Unlocks a mutex.

Since: 10

Parameters

NameDescription
ffrt_mutex_t* mutexPointer to the mutex.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex is unlocked;
returns ffrt_error_inval otherwise.

ffrt_mutex_trylock()

FFRT_C_API int ffrt_mutex_trylock(ffrt_mutex_t* mutex)

Description

Attempts to lock a mutex.

Since: 10

Parameters

NameDescription
ffrt_mutex_t* mutexPointer to the mutex.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex is locked;
returns ffrt_error_inval or ffrt_error_busy otherwise.

ffrt_mutex_destroy()

FFRT_C_API int ffrt_mutex_destroy(ffrt_mutex_t* mutex)

Description

Destroys a mutex.

Since: 10

Parameters

NameDescription
ffrt_mutex_t* mutexPointer to the mutex.

Returns

TypeDescription
FFRT_C_API intReturns ffrt_success if the mutex is destroyed successfully;
returns ffrt_error_inval otherwise.

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-fiber-h

openharmony 鸿蒙 capi-ffrt-ffrt-mutex-t

openharmony 鸿蒙 capi-ffrt-ffrt-rwlockattr-t

openharmony 鸿蒙 capi-loop-h

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 capi-ffrt-ffrt-rwlock-t

openharmony 鸿蒙 capi-shared-mutex-h

openharmony 鸿蒙 capi-ffrt-ffrt-queue-attr-t

openharmony 鸿蒙 capi-ffrt

openharmony 鸿蒙 capi-ffrt-ffrt-fiber-t

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/OGP1JCeR