1 Star 0 Fork 1

zhifanhe/fastspice_reveiw

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lock_test1.c 712 Bytes
一键复制 编辑 原始数据 按行查看 历史
zhifanhe 提交于 4年前 . review
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int num = 0; //设置为全局变量,在全局区域,共享
void* myfun(void* arg);
pthread_mutex_t mtx;
int main()
{
pthread_t id[2];
pthread_mutex_init(&mtx,NULL);
for (int i = 0; i < 2; i++) {
pthread_create(&(id[i]), NULL, myfun, NULL);
// printf("mian: %d : %d \n", num,pthread_self());
}
pthread_join(id[0],NULL);
pthread_join(id[1],NULL);
printf("%d",num);
return 0;
}
void* myfun(void* arg)
{
int i;
for(i=0;i<100000000;i++){
// pthread_mutex_lock(&mtx);
num++;
// pthread_mutex_unlock(&mtx);
// printf("%d : %d \n",num,pthread_self());
}
return NULL;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/he_zhifan/fastspice_reveiw.git
git@gitee.com:he_zhifan/fastspice_reveiw.git
he_zhifan
fastspice_reveiw
fastspice_reveiw
master

搜索帮助