飞翔飞翔
主页
  • 计算机基础

    • TCP/IP协议
    • Linux命令
  • 数据库

    • SQL教程
  • 编程语言

    • C语言
    • Python2
    • Python3
  • 数据格式

    • JSON教程
  • 工具

    • Markdown指南
  • Git

    • GitFlow
  • Quartz

    • Quartz教程
  • Java

    • Java设计模式
  • 缓存

    • Redis教程
联系
阿里云
主页
  • 计算机基础

    • TCP/IP协议
    • Linux命令
  • 数据库

    • SQL教程
  • 编程语言

    • C语言
    • Python2
    • Python3
  • 数据格式

    • JSON教程
  • 工具

    • Markdown指南
  • Git

    • GitFlow
  • Quartz

    • Quartz教程
  • Java

    • Java设计模式
  • 缓存

    • Redis教程
联系
阿里云
  • 学习路径
  • 第1章 编程基础概念

    • 冯·诺依曼体系结构
    • 数据在计算机中的表示
    • 编程语言的层次
    • C语言的起源与发展
    • C99标准的主要改进
    • 开发环境搭建
    • 第一个C程序
    • 编译与运行流程
    • 可移植性风险的三级体系
  • 第2章 数据类型与运算

    • 字符集与标识符
    • 关键字
    • 注释
    • char 类型
    • short 与 int
    • long 与 long long
    • 有符号与无符号
    • 取值范围与 limits.h
    • float 与 double
    • long double
    • _Bool 类型
    • 变量声明与定义
    • 常量
    • 转义序列
    • 算术运算符
    • 赋值运算符
    • 自增自减运算符
    • 关系与判等运算符
    • 逻辑运算符
    • 位运算符
    • 条件运算符
    • 逗号运算符
    • 运算符优先级
    • 隐式类型转换
    • 显式类型转换
  • 第3章 控制流

    • 表达式语句与空语句
    • 复合语句
    • if 语句
    • switch 语句
    • while 循环
    • do-while 循环
    • for 循环
    • break 与 continue
    • goto 语句
    • return 语句
  • 第4章 函数与模块化编程

    • 函数定义
    • 函数声明与原型
    • main 函数
    • 函数调用机制
    • 传值调用
    • 数组参数
    • 作用域
    • 存储期
    • 链接属性
    • static 与 extern
    • 递归
    • 头文件与源文件
    • 头文件保护
    • include 规则
  • 第5章 数组与字符串

    • 一维数组声明与初始化
    • 数组的存储模型
    • 数组访问与越界
    • 数组操作
    • 二维数组
    • 变长数组 VLA
    • 字符串基础
    • 字符串输入输出
    • 字符串处理函数
    • 字符串与数字转换
  • 第6章 指针

    • 指针的概念
    • 指针的声明与使用
    • 指针运算
    • const 与指针
    • 数组名与指针
    • 指针遍历数组
    • 指针与多维数组
    • 指针作为函数参数
    • 函数返回指针
    • 函数指针
    • 二级指针
    • 复杂声明解析
  • 第7章 结构体、联合体与枚举

    • 结构体定义与声明
    • 结构体初始化
    • 结构体成员访问
    • 结构体嵌套
    • 结构体指针
    • 结构体与函数
    • 联合体
    • 联合体与类型双关
    • 枚举类型
    • 位域
    • 内存对齐与填充
  • 第8章 动态内存管理

    • malloc 与 free
    • calloc 与 realloc
    • 内存泄漏
    • 悬垂指针
    • 内存分配策略
    • 自定义内存池
    • Valgrind 与内存检测
    • 内存碎片
    • 内存对齐分配
    • 常见内存错误
  • 第9章 文件输入输出

    • 文件打开与关闭
    • 文本读写
    • 格式化输入输出
    • 二进制读写
    • 文件定位
    • 错误处理
    • 标准流
    • 临时文件
    • 文件操作示例
  • 第10章 预处理器

    • 预处理器基础
    • 宏定义
    • 带参数的宏
    • 条件编译
    • 头文件包含
    • 预定义宏
    • 宏的高级技巧
    • 预处理器陷阱
    • 编译器特定扩展
  • 第11章 标准库概览

    • 标准库概述
    • assert.h
    • ctype.h
    • errno.h
    • float.h
    • limits.h
    • locale.h
    • math.h
    • setjmp.h
    • signal.h
    • stdarg.h
    • stddef.h
    • stdlib.h
  • 第12章 进阶主题

    • 内联函数
    • 变长数组 VLA
    • 复数类型
    • 布尔类型
    • stdint 与 inttypes
    • 灵活数组成员
    • 匿名结构体与联合体
    • 静态断言
    • 线程支持
    • 原子操作

线程支持

C11 引入标准线程支持 <threads.h>,提供跨平台的线程创建、同步、互斥等功能。在此之前,线程支持依赖平台特定 API(POSIX pthreads、Windows threads)。C11 线程库简化了多线程编程,但支持程度取决于编译器和平台。

基本线程创建

#include <threads.h>
#include <stdio.h>

int thread_func(void *arg)
{
    int num = *(int *)arg;
    printf("Thread %d running\n", num);
    return 0;
}

int main(void)
{
    thrd_t thread;
    int arg = 1;
    
    if (thrd_create(&thread, thread_func, &arg) == thrd_success) {
        thrd_join(thread, NULL);    /* 等待线程结束 */
    }
    
    return 0;
}

互斥锁

#include <threads.h>

static mtx_t mutex;
static int counter = 0;

int increment(void *arg)
{
    for (int i = 0; i < 1000; i++) {
        mtx_lock(&mutex);
        counter++;
        mtx_unlock(&mutex);
    }
    return 0;
}

int main(void)
{
    mtx_init(&mutex, mtx_plain);
    
    thrd_t t1, t2;
    thrd_create(&t1, increment, NULL);
    thrd_create(&t2, increment, NULL);
    
    thrd_join(t1, NULL);
    thrd_join(t2, NULL);
    
    printf("Counter: %d\n", counter);   /* 2000 */
    
    mtx_destroy(&mutex);
    return 0;
}

条件变量

#include <threads.h>

static mtx_t mutex;
static cnd_t cond;
static int ready = 0;

int worker(void *arg)
{
    mtx_lock(&mutex);
    while (!ready) {
        cnd_wait(&cond, &mutex);    /* 等待条件 */
    }
    printf("Worker proceeding\n");
    mtx_unlock(&mutex);
    return 0;
}

int main(void)
{
    mtx_init(&mutex, mtx_plain);
    cnd_init(&cond);
    
    thrd_t worker_thread;
    thrd_create(&worker_thread, worker, NULL);
    
    /* 模拟工作 */
    thrd_sleep(&(struct timespec){.tv_sec = 1}, NULL);
    
    mtx_lock(&mutex);
    ready = 1;
    cnd_signal(&cond);              /* 通知等待线程 */
    mtx_unlock(&mutex);
    
    thrd_join(worker_thread, NULL);
    
    mtx_destroy(&mutex);
    cnd_destroy(&cond);
    return 0;
}

线程局部存储

_Thread_local int thread_local_var;     /* C11 */

/* 或 */
thread_local int tls_var;               /* <threads.h> 宏 */

兼容性

/* 如果编译器不支持 C11 threads */
#ifdef __STDC_NO_THREADS__
    /* 使用平台特定 API */
    #include <pthread.h>
    /* ... */
#else
    #include <threads.h>
    /* ... */
#endif

常见错误

忘记解锁:

mtx_lock(&mutex);
/* 操作 */
if (error())
    return -1;                  /* 错误:未解锁就返回 */
mtx_unlock(&mutex);

/* 正确 */
mtx_lock(&mutex);
/* 操作 */
mtx_unlock(&mutex);
if (error())
    return -1;

条件变量虚假唤醒:

/* 错误 */
if (!ready) {                   /* 应该用 while */
    cnd_wait(&cond, &mutex);
}

/* 正确 */
while (!ready) {                /* 防止虚假唤醒 */
    cnd_wait(&cond, &mutex);
}

最佳实践

  • 锁的粒度尽量小
  • 用 while 而非 if 检查条件变量
  • 避免死锁(统一加锁顺序)
  • 线程局部存储减少共享数据
  • 考虑无锁数据结构
上一页
静态断言
下一页
原子操作