c/c++原子操作 接口函数 自查文档
来源:互联网 发布:数据采集监控 编辑:程序博客网 时间:2024/06/08 10:22
C:
type __sync_fetch_and_add (type *ptr, type value);type __sync_fetch_and_sub (type *ptr, type value);type __sync_fetch_and_or (type *ptr, type value);type __sync_fetch_and_and (type *ptr, type value);type __sync_fetch_and_xor (type *ptr, type value);type __sync_fetch_and_nand (type *ptr, type value);type __sync_add_and_fetch (type *ptr, type value);type __sync_sub_and_fetch (type *ptr, type value);type __sync_or_and_fetch (type *ptr, type value);type __sync_and_and_fetch (type *ptr, type value);type __sync_xor_and_fetch (type *ptr, type value);type __sync_nand_and_fetch (type *ptr, type value);bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)type __sync_lock_test_and_set (type *ptr, type value, ...) 将*ptr设为value并返回*ptr操作之前的值。void __sync_lock_release (type *ptr, ...) 将*ptr置0
#include <stdio.h> #include <pthread.h> #include <stdlib.h> static int count = 0; void *test_func(void *arg) { int i=0; for(i=0;i<20000;++i){ __sync_fetch_and_add(&count,1); } returnNULL; } int main(int argc, const char *argv[]) { pthread_tid[20]; int i =0; for(i=0;i<20;++i){ pthread_create(&id[i],NULL,test_func,NULL); } for(i=0;i<20;++i){ pthread_join(id[i],NULL); } printf("%d\n",count); return0; }
c++:查看
http://en.cppreference.com/w/cpp/atomic/atomic
std::atomic_char std::atomic<char>std::atomic_schar std::atomic<signed char>std::atomic_uchar std::atomic<unsigned char>std::atomic_short std::atomic<short>std::atomic_ushort std::atomic<unsigned short>std::atomic_int std::atomic<int>std::atomic_uint std::atomic<unsigned int>std::atomic_long std::atomic<long>std::atomic_ulong std::atomic<unsigned long>std::atomic_llong std::atomic<long long>std::atomic_ullong std::atomic<unsigned long long>
#include <iostream>#include <thread>#include <atomic> // modifiedstd::atomic_long sum = {0L}; // modifiedvoid fun(){ for(int i=0;i<100;++i) sum += i;}int main(){ std::cout << "Before joining,sun = " << sum << std::endl; std::thread t1(fun); std::thread t2(fun); t1.join(); t2.join(); std::cout << "After joining,sun = " << sum << std::endl;}
0 0
- c/c++原子操作 接口函数 自查文档
- C/C++-------------__sync_fetch_and_add 原子操作------------------
- [C++]gcc中的原子操作
- [C/C++]_[初级]_[原子操作]
- C程序接口设计与实现-----原子
- LINUX C 字符串操作函数接口 汇总
- C语言赋值语句是不是原子操作?
- C语言赋值语句是不是原子操作?
- C语言赋值语句是不是原子操作?
- C++,多线程:原子操作解决线程冲突
- [C/C++]_[macOS-Windows]_[原子操作2]
- Windows Via C/C++:用户模式下的线程同步——原子操作:Interlocked函数族
- Windows Via C/C++:用户模式下的线程同步——原子操作:Interlocked函数族
- 读书笔记--C语言接口与实现--atom(原子)
- C 语言字符串操作函数API文档说明
- C函数接口
- redis C接口函数
- 基于C语言的SQLite操作接口函数简介
- AIDL 进程间通信
- Hello World_Linux篇——测试gcc和g++是否可用
- vb中的十六进制
- java jar服务部署脚本编写
- CSV模块使用时的几个问题及解决方法
- c/c++原子操作 接口函数 自查文档
- idea编辑器注册方法
- 冒泡排序法---vb
- 添加shell命令别名
- Run Loop是什么,使用的目的,何时使用和关注点
- angularJs初学遇到的坑之directive
- jQuery入门(1) 加载事件
- tomact web.xml配置参考
- SQL学习笔记2——用代码创建、删除表