分支预测建议:

http://www.cppblog.com/mysileng/archive/2014/09/29/208454.html

#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif

内存对齐:

这个表达式的意思就是 d/a 向上取整(a是2的倍数)。也就是说 如果表达式的结果是n那么
(n - 1) *a<d <= n * a,
如果按a对齐那么至少要申请n这么大才能把d放里面。
#define align_ptr(p, a)        (uint8_t*)(((uintptr_t)(p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
#define align(d, a) (((d) + (a - 1)) & ~(a - 1))

属性暗示:

http://www.cnblogs.com/sunyubo/archive/2010/12/20/2282084.html

#define ignore(exp)            {int ignore __attribute__ ((unused)) = (exp);}

// 内存对齐
struct my_unpacked_struct
{
char c;
int i;
};
struct my_packed_struct
{
char c;
int i;
struct my_unpacked_struct s; }__attribute__ ((__packed__));

汇编与锁:

http://blog.csdn.net/lu_ming/article/details/4984115

http://blog.csdn.net/maotianwang/article/details/9154159

/ 32位系统
static __inline__ void atomic32_add(volatile int32_t *v, int i) {
__asm__ __volatile__("lock;" "addl %1,%0"
: "=m" ((*v)) : "r" (i), "m" ((*v)));
}
static __inline__ int32_t atomic32_add_return(volatile int32_t *value, int32_t diff)
{
int32_t old = diff;
__asm__ volatile (
"lock;" "xaddl %0, %1"
:"+r" (diff), "+m" (*value) : : "memory");
return diff + old;
}
static __inline__ void atomic32_inc(volatile int32_t *value *v)
{
__asm__ __volatile__("lock;" "incl %0" : "=m" (*v) :"m" (*v));
}
static __inline__ void atomic32_dec(volatile int32_t *value*v)
{
__asm__ __volatile__("lock;" "decl %0" : "=m" (*v) :"m" (*v));
}
static __inline__ int32_t atomic_cmp_set(volatile int32_t *lock, int32_t old, int32_t set)
{
uint8_t res;
__asm__ volatile (
"lock;" "cmpxchgl %3, %1; sete %0"
: "=a" (res) : "m" (*lock), "a" (old), "r" (set) : "cc", "memory");
return res;
}
//
#define trylock(lock) (*(lock) == 0 && atomic_cmp_set(lock, 0, 1))
#define unlock(lock) {__asm__ ("" ::: "memory"); *(lock) = 0;}
// 自旋lock
static __inline__ void spin_lock(volatile int32_t *lock)
{
  int i,n;
for(;;) {
if(*lock == 0 && atomic_cmp_set(lock, 0, 1)) {
return;
   } for(n = 1; n < 1024; n <<= 1) {
for(i = 0; i < n; i++) {
__asm__ (".byte 0xf3, 0x90");
} if(*lock == 0 && atomic_cmp_set(lock, 0, 1)) {
return;
  }
}
}
//?? sched_yield();
}
#define spin_unlock unlock

  

菜菜CPP日记的更多相关文章

  1. Cmake新手使用日记(1)【C++11下的初体验】

    第一次使用Cmake,搜索了很多使用教程,包括<Cmake实践>.<Cmake手册>等,但是在针对最新的C++11条件下编程还是会存在一点点问题,需要实验很多次错误并搜索大量文 ...

  2. ArcGIS 10.5 tensorflow安装日记

    ArcGIS 10.5 tensorflow安装日记 商务科技合作:向日葵,135-4855__4328,xiexiaokui#qq.com Datetime: 2019年5月27日星期一 Os: w ...

  3. VS Code 调教日记(2022.6.26更新)

    VS Code 调教日记(2022.6.26更新) 基于msys2的MinGW-w64 GCC的环境配置 下载并安装msys2 到路径...msys2安装路径...\msys64\etc\pacman ...

  4. 微信小程序开发日记——高仿知乎日报(下)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP 要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该 ...

  5. 微信小程序开发日记——高仿知乎日报(中)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该教 ...

  6. 微信小程序开发日记——高仿知乎日报(上)

    本人对知乎日报是情有独钟,看我的博客和github就知道了,写了几个不同技术类型的知乎日报APP 要做微信小程序首先要对html,css,js有一定的基础,还有对微信小程序的API也要非常熟悉 我将该 ...

  7. Linux学习日记-使用EF6 Code First(四)

    一.在linux上使用EF 开发环境 VS2013+mono 3.10.0 +EF 6.1.0 先检测一下EF是不是6的 如果不是  请参阅 Linux学习日记-EF6的安装升级(三) 由于我的数据库 ...

  8. 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...

  9. Json CPP 中文支持与入门示例

    在每一个Json Cpp自带*.cpp文件头加上: #include "stdafx.h" 将Json Cpp对自带的头文件的引用修改为单引号方式,例如json_reader.cp ...

随机推荐

  1. soapui中文操作手册(八)----Web服务的功能测试案例

    现在,让我们来看看在一个TestCase的功能测试. 展开 Simple TestSuite并双击Simple Login and Logout w. Properties Steps. 正如你所看到 ...

  2. 【转】crontab定时任务中文乱码问题

    转载:http://blog.163.com/rettar@126/blog/static/1216503422012135511740/ 手动执行都很正常的的脚步,添加到定时任务中一直执行失败,日志 ...

  3. next_permutation函数

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类 ...

  4. Leetcode Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. 警惕自己,不断学习c++【转】

    每天早上起床看一遍,时刻警惕自己,每天至少要浏览http://www.cplusplus.com 1.把C++当成一门新的语言学习(和C没啥关系!真的.):2.看<Thinking In C++ ...

  6. UIAlertView 与 UIActionSheet (提示用户)的使用方法

    UIAlertView 提示用户  帮助用户选择框 //    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...

  7. cdoj 树上战争(Battle on the tree) Label:并查集?

    给一棵树,如果树上的某个节点被某个人占据,则它的所有儿子都被占据,lxh和pfz初始时分别站在两个节点上,谁当前所在的点被另一个人占据,他就输了比赛,问谁能获胜. Input 输入包含多组数据 每组第 ...

  8. word-wrap和word-break的区别

    最初只有word-wrap,当指定word-wrap: break-word;时将导致容器内的长单词换行且被切断. 后来IE发明了word-break:当应用word-break:break-all时 ...

  9. C#_数据转换 实用方法

    [String转Array]string str = "123asd456asd789";单字符: string[] a0 = str.Split('a');多字符: string ...

  10. Linux下rar命令详解

    Linux下rar命令详解 用法: rar <命令> -<选项1> ….-<选项N> < 操作文档> <文件…> <@文件列表…> ...