随笔分类 - 编程技巧
C、C++、Java、Python、Lua等编程语言的使用方法和技巧。
摘要:#include int thread_flag;pthread_cond_t thread_flag_cv;pthread_mutex_t thread_flag_mutex;void initialize_flag (){/* Initialize the mutex and condition variable.pthread_mutex_init (&thread_flag_mutex, NULL);pthread_cond_init (&thread_flag_cv, NULL);/* Initialize the flag value. */thread_flag
阅读全文
摘要:#include #include #include struct job {/* Link field for linked list.struct job* next;*//* Other fields describing work to be done... */};/* A linked list of pending jobs.struct job* job_queue;*//* A mutex protecting job_queue. */pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;/* A semap
阅读全文
摘要:[(lambda x: x*x)(x) for x in range(10)]Or better yet:[x*x for x in range(10)]
阅读全文
摘要:约瑟夫环问题: 输入:1)总共人数;2)每次被杀的序号数; 输出:最后一个活着的序号python代码如下:n=int (input('please input the number of people:') )k=int (input('please input the discard number:'))a=[]for i in range(n): a.append(i+1)print 'all the serial number of people:'print ai=0j=1while len(a)>1: if j==k: d...
阅读全文
摘要:#includeusing namespace std;//1-insert sort methodvoid insert_sort( int *arr, int n ){ int i, k; for( i=1; i0; --k ) { if( arr[k] 0; --k ) { for( i=0; i arr[i+1] ) { int tm...
阅读全文
摘要:基类为抽象类,在不同的动态库中实现不同的执行行为,但是每个动态库要提供2个统一的方法:1) baseClass * create(); 2) void destroy( baseClass* );,调用该实际类的上下文,通过dlopen,dlsym( dl, "create"), dlsym( dl, "destroy")来获得实际对象的句柄。实际上是一种工厂/builder模型。1. 基类//base.h#include class baseClass {public: virtual void test(){}; virtual ~baseClass
阅读全文
摘要:char* urlencode(const void* buf, size_t size) { _assert_(buf && size = 'A' && c = 'a' && c = '0' && c > 4; if (num = ep) break; c = *str; if (c >= '0' && c = 'a' && c = 'A' && c = ep) break;
阅读全文
摘要:一个数据矩阵,选定某个位置的数据元作为参考,寻找与该数据元值相同的连成一片的最大范围。如下二维数组中所示,选择arr[1][3]作为参考对象,从位置(1,3)开始寻找所有相连的值为‘1’的元素。
阅读全文
摘要:http://blog.csdn.net/allenlinrui/article/details/5964046
阅读全文
摘要:假设我们写了一个C代码文件 code.c包含下面代码:int accum = 0;int sum(int x, int y){ int t = x + y; accum += t; return t;} 这是用echo命令输入源码的效果,简单的就是最好的:) 一、查看GCC生成的汇编代码 在命令行上用“-S”选项,就能看到C编译器产生的汇编代码: #gcc -S code.c 注意:这里是大写的-S,如果用小写gcc会说找不到main函数 会在当前目录下生成code.s文件,直接打开即可 这段汇编代码没有经过优化: .file "code.c".g...
阅读全文
摘要:http://stackoverflow.com/questions/501105/comm
阅读全文
摘要:1)RedisClient的SetValue(string key, byte[]val)方法,如果val的长度为0,则redis服务器库中设置该key值失败。2)PoolRedisClientManager显然是针对sentinel服务集群的访问封装,但是经过试用,还发现一些不足之处: a、不能自动动态发现master与slave; b、对于多个master的架构访问缺少设计。
阅读全文