# pthread_example **Repository Path**: timing_matlab/pthread_example ## Basic Information - **Project Name**: pthread_example - **Description**: 一些简单的多线程例子代码 - **Primary Language**: C - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-11 - **Last Updated**: 2025-04-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # pthread_example #### 介绍 一些简单的多线程例子代码 #### 软件架构 多线程程序 #### 1.pthread.h thread ---- 共享内存 process ---- 没有共享内存 ``` #include #include #include void* myfunc(void* args) { printf("Hello!World!\n"); return NULL; } int main(void) { pthread_t th; pthread_create(&th,NULL,myfunc,NULL); pthread_join(th,NULL); return 0; } ``` #### 2.锁 mutex ``` #include //创建锁变量 pthreat_mutex_t lock; //初始化或者使能锁 pthread_mutex_init(&lock,NULL); //使用锁,锁住一段代码 pthread_mutex_lock(&lock); // ...... 一段代码 //解锁 pthread_mutex_unlock(&lock); ``` #### 3.假共享 false sharing 局部变量与数组 内存 ----> 缓存(加快读取的效率) 在多核CPU缓存里面,数字不一致,为了同步 导致时间的延误,这种称为假共享