• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

twilight0966

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

2025-2026-1 20231301 《信息安全设计》第十二周学习总结

2025-2026-1 20231301 《信息安全设计》第十二周学习总结

目录
  • 作业信息
  • 学习内容总结
    • 线程编程深度解析
      • 1. 线程 vs 进程对比
      • 2. 完整的线程安全示例
      • 3. 线程池实现
    • 高级同步机制
      • 读写锁应用
    • 思维导图

作业信息

作业 链接
作业课程 <班级>(2025-2026-1 信息安全设计)
作业要求 <作业>(2025-2026-1 信息安全设计 预习作业要求)
作业目标 《Head First C 嗨翻C语⾔》> 预习第十二章
作业正文 <博客>(第十二周学习总结)

学习内容总结

第十二章:多线程编程

线程编程深度解析

1. 线程 vs 进程对比

特性 进程 线程
创建开销 大 小
内存空间 独立 共享
通信机制 复杂(IPC) 简单(共享内存)
上下文切换 慢 快
独立性 完全独立 相互依赖

2. 完整的线程安全示例

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define NUM_THREADS 5
#define NUM_OPERATIONS 100000

// 共享资源
typedef struct {
    int balance;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} bank_account;

bank_account account = {1000, PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER};

// 线程安全的存款操作
void deposit(int amount) {
    pthread_mutex_lock(&account.mutex);
    
    int old_balance = account.balance;
    usleep(100); // 模拟处理延迟
    account.balance = old_balance + amount;
    
    printf("Deposited %d, new balance: %d\n", amount, account.balance);
    
    // 通知等待的线程
    pthread_cond_broadcast(&account.cond);
    pthread_mutex_unlock(&account.mutex);
}

// 线程安全的取款操作
int withdraw(int amount) {
    pthread_mutex_lock(&account.mutex);
    
    // 等待直到有足够余额
    while (account.balance < amount) {
        printf("Insufficient funds. Waiting...\n");
        pthread_cond_wait(&account.cond, &account.mutex);
    }
    
    int old_balance = account.balance;
    usleep(100); // 模拟处理延迟
    account.balance = old_balance - amount;
    
    printf("Withdrew %d, new balance: %d\n", amount, account.balance);
    
    pthread_mutex_unlock(&account.mutex);
    return amount;
}

void* customer_thread(void* arg) {
    long thread_id = (long)arg;
    
    for (int i = 0; i < NUM_OPERATIONS; i++) {
        if (i % 2 == 0) {
            deposit(100);
        } else {
            withdraw(50);
        }
    }
    
    printf("Thread %ld finished\n", thread_id);
    return NULL;
}

void demonstrate_thread_safety() {
    pthread_t threads[NUM_THREADS];
    
    // 创建多个客户线程
    for (long i = 0; i < NUM_THREADS; i++) {
        if (pthread_create(&threads[i], NULL, customer_thread, (void*)i) != 0) {
            perror("pthread_create");
            exit(1);
        }
    }
    
    // 等待所有线程完成
    for (int i = 0; i < NUM_THREADS; i++) {
        pthread_join(threads[i], NULL);
    }
    
    printf("Final balance: %d\n", account.balance);
    
    // 清理资源
    pthread_mutex_destroy(&account.mutex);
    pthread_cond_destroy(&account.cond);
}

3. 线程池实现

#include <pthread.h>
#include <semaphore.h>

#define THREAD_POOL_SIZE 4
#define QUEUE_SIZE 100

typedef struct {
    void (*function)(void*);
    void* argument;
} task_t;

typedef struct {
    task_t tasks[QUEUE_SIZE];
    int front, rear, count;
    pthread_mutex_t lock;
    sem_t tasks_available;
    sem_t spaces_available;
} task_queue_t;

typedef struct {
    pthread_t threads[THREAD_POOL_SIZE];
    task_queue_t queue;
    int shutdown;
} thread_pool_t;

void task_queue_init(task_queue_t* queue) {
    queue->front = queue->rear = queue->count = 0;
    pthread_mutex_init(&queue->lock, NULL);
    sem_init(&queue->tasks_available, 0, 0);
    sem_init(&queue->spaces_available, 0, QUEUE_SIZE);
}

int task_queue_enqueue(task_queue_t* queue, task_t task) {
    sem_wait(&queue->spaces_available);
    pthread_mutex_lock(&queue->lock);
    
    queue->tasks[queue->rear] = task;
    queue->rear = (queue->rear + 1) % QUEUE_SIZE;
    queue->count++;
    
    pthread_mutex_unlock(&queue->lock);
    sem_post(&queue->tasks_available);
    return 0;
}

int task_queue_dequeue(task_queue_t* queue, task_t* task) {
    sem_wait(&queue->tasks_available);
    pthread_mutex_lock(&queue->lock);
    
    *task = queue->tasks[queue->front];
    queue->front = (queue->front + 1) % QUEUE_SIZE;
    queue->count--;
    
    pthread_mutex_unlock(&queue->lock);
    sem_post(&queue->spaces_available);
    return 0;
}

void* worker_thread(void* arg) {
    thread_pool_t* pool = (thread_pool_t*)arg;
    task_t task;
    
    while (!pool->shutdown) {
        if (task_queue_dequeue(&pool->queue, &task) == 0) {
            task.function(task.argument);
        }
    }
    
    return NULL;
}

void thread_pool_init(thread_pool_t* pool) {
    pool->shutdown = 0;
    task_queue_init(&pool->queue);
    
    for (int i = 0; i < THREAD_POOL_SIZE; i++) {
        pthread_create(&pool->threads[i], NULL, worker_thread, pool);
    }
}

void thread_pool_submit(thread_pool_t* pool, void (*function)(void*), void* arg) {
    task_t task = {function, arg};
    task_queue_enqueue(&pool->queue, task);
}

void thread_pool_shutdown(thread_pool_t* pool) {
    pool->shutdown = 1;
    
    // 唤醒所有等待的线程
    for (int i = 0; i < THREAD_POOL_SIZE; i++) {
        sem_post(&pool->queue.tasks_available);
    }
    
    for (int i = 0; i < THREAD_POOL_SIZE; i++) {
        pthread_join(pool->threads[i], NULL);
    }
}

高级同步机制

读写锁应用

#include <pthread.h>

typedef struct {
    pthread_rwlock_t rwlock;
    int data;
    int version;
} shared_data_t;

void reader_thread(shared_data_t* shared) {
    pthread_rwlock_rdlock(&shared->rwlock);
    
    // 多个读取者可以同时访问
    printf("Reader: data=%d, version=%d\n", shared->data, shared->version);
    
    pthread_rwlock_unlock(&shared->rwlock);
}

void writer_thread(shared_data_t* shared, int new_data) {
    pthread_rwlock_wrlock(&shared->rwlock);
    
    // 只有一个写入者可以访问
    shared->data = new_data;
    shared->version++;
    printf("Writer: updated data to %d, version=%d\n", new_data, shared->version);
    
    pthread_rwlock_unlock(&shared->rwlock);
}

思维导图

截屏2025-09-30 下午10.06

posted on 2025-09-30 22:08  20231301周子昂  阅读(6)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3