最小 pthread mutex 示例

pthread_example.c
#include <pthread.h>
int main() {
    // 创建互斥锁
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, NULL /* 默认互斥锁属性 */);
    // 加锁
    pthread_mutex_lock(&mutex);
    // 解锁
    pthread_mutex_unlock(&mutex);
    // 清理
    pthread_mutex_destroy(&mutex);
}

像这样编译:

compile_pthread.sh
gcc -o main main.c -pthread

Check out similar posts by category: C/C++