c/c++原子操作 接口函数 自查文档
2016-05-19 17:42
495 查看
C:
c++:查看
http://en.cppreference.com/w/cpp/atomic/atomic
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> // modified std::atomic_long sum = {0L}; // modified void 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; }
相关文章推荐
- C/C++的字符串操作(一)
- C++第六次作业 项目1 项目4
- C语言中memset函数详解
- 剖析c++(二) 内置类型的内存形式
- 剖析c++(一) 进程内存空间布局
- C++笔记
- 第12周项目1--实现复数类中的运算重载(1)
- C/C++的一些杂项
- 模板
- 亚像素图像图像平移
- 常对象及常成员及静态数据成员和静态成员函数的学习内容。
- C++拷贝构造函数(深拷贝,浅拷贝)
- 杭电1000、1001C语言,通过。
- c/c++代码中执行cmd命令
- C语言常见字符串面试题
- 【C/C++】C++基础知识查漏补缺
- C语言字符串操作函数
- C++ erase()用法技巧
- 输入输出(IO)简介
- 单词翻转(4种思路,C/C++代码)