第4章 原子操作
发布日期:2021-06-30 18:59:10
浏览次数:2
分类:技术文章
本文共 1980 字,大约阅读时间需要 6 分钟。
#include#include #include #include #include #define gettidv1() syscall(__NR_gettid) #define gettidv2() syscall(SYS_gettid) long int NUM = 0; void * th(void * ptr) { int i = 0; for(i = 0;i < 1000000000;i++) { NUM++; } //sleep(1); //printf("IM %s ID:%ld number:%ld\n",(char*)ptr,gettidv1(),NUM); } int main(int argc,char ** argv) { pthread_t thread1 = -1; if(pthread_create(&thread1,NULL,th,"th1")!=0) { printf("thread1 creat error\n"); } pthread_t thread2 = -1; if(pthread_create(&thread2,NULL,th,"th2")!=0) { printf("thread1 creat error\n"); } while(1) { printf("IM %s ID:%ld number:%ld\n","Main",gettidv1(),NUM); sleep(2); }; }
all: gcc thread.c -o thread -pthread clean: rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
linux@ubuntu:~/linuxBook/linux-c/pthread$ make && ./thread gcc thread.c -o thread -pthread IM Main ID:7503 number:0 IM Main ID:7503 number:638925977 IM Main ID:7503 number:1334751130 IM Main ID:7503 number:1997474779
在单核CPU上应该很好理解,但是在多核上要费点心思,SMP加上L1,L2缓存后,处理变得很复杂。
之前文章有点长,以后还是简化文章,觉得不错,支持一下。
转载地址:https://linus.blog.csdn.net/article/details/97622108 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月08日 14时31分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tf callbacks
2019-04-30
keras、tf、numpy实现logloss对比
2019-04-30
Ubuntu20.04安装微信
2019-04-30
Restful风格的使用
2019-04-30
Swagger基础入门整合SpringBoot
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30
攻防世界web进阶区NewsCenter详解
2019-04-30
攻防世界web进阶PHP2详解
2019-04-30
如何解决词达人问题(新)
2019-04-30
攻防世界web进阶区surpersqli详解
2019-04-30
攻防世界web进阶区easytornado详解
2019-04-30
攻防世界web进阶区web2详解
2019-04-30
xss-labs详解(上)1-10
2019-04-30
xss-labs详解(下)11-20
2019-04-30
攻防世界web进阶区ics-05详解
2019-04-30
攻防世界web进阶区FlatScience详解
2019-04-30
攻防世界web进阶区ics-04详解
2019-04-30
攻防世界web进阶区Cat详解
2019-04-30
攻防世界web进阶区bug详解
2019-04-30
攻防世界web进阶区ics-07详解
2019-04-30