Fork me on GitHub
一共有 61 篇文章,分页:12 / 13

发现锁定Dictionary的时候,它的属性Keys并没有被锁定

前几天在压力测试的时候发现的并发问题,记录一下 发现锁定Dictionary的时候,它的属性Keys并没有被锁定(不知道Values是不是也一样?),在遍历Keys的时候还是会报值已修改,无法遍历的错误。 如下代码,横线处会引发错误:

public static void CloneKeyTo(Dictionary<string, object> sourceASO, out Dictionary<string, object> desASO)  
{  
    desASO = new Dictionary<string, object>();  

    lock (sourceASO)  
    {  
        foreach (string key in sourceASO.Keys)
        {  
            desASO[key] = sourceASO[key];  
        }  
    }  
}

解决方案如下:

public static void CloneKeyTo(Dictionary<string, object> sourceASO, out Dictionary<string, object> desASO)  
{  
    desASO = new Dictionary<string, object>();  

    string[] keys;  
    lock (sourceASO.Keys)  
    {  
        keys = new string[sourceASO.Keys.Count];  
        sourceASO.Keys.CopyTo(keys, 0);  
    }  
    foreach (string key in keys)  
    {  
        lock (sourceASO)  
        {  
            if (sourceASO.ContainsKey(key))  
            {  
                desASO[key] = sourceASO[key];  
            }  
        }  
    }  
}

AS3的一些压缩解压缩类库(AS3 ZIP、AS3 GZIP等等)

在现在开发的游戏中,由于战斗数据比较大,所以尝试对战斗数据进行压缩,然后输出到客户端flash端再解压。 Google到一篇文章,对照翻译工具翻译一下 :)

在我的一些项目中,经常需要对数据做一些转换操作,所以积累一些很有意思的用于数据压缩/解压缩的第三方类库。 当然ByteArray类本身就带了数据压缩和解压缩的方法,可以用在flash player使用zlib算法和AIR程序使用多种算法。在FLASH跟PHP作为后台的编程中,我后来选择了ByteArray的compress方法来做zlib算法的压缩,用这个方法用的比较顺手,而且很快。 下面是一些第三方的类库地址以及介绍:

  • AS3 Zip: AS 3 下用来读取和写入zip文件的类库
  • FZip: FZip 是一个用于AS 3 下读取、创建、修改zip压缩包的类库
  • ASZip: AS 3 用于创建zip文件的类库
  • LZMA Encoder: AS3下使用LZMA算法压缩数据的类库.
  • LZMA Decoder: 跟上面类库对应的用于LZMA算法解压缩数据.
  • AsCompress: AS3下 GZIP压缩和解压缩类库,好像需要SDK版本在4.x以上,flash cs3下不可用。
  • Gzip for HTTPService/URLLoader: 给你的 Flex/AIR HTTPService/URLLoader增加gzip支持
  • airxzip: AIR的zip类库

如果你还知道更多的类库或者其他好东东,欢迎告知!

翻译自://blog.yoz.sk/2011/01/quick-tip-compression-in-flash/

While working on one of my projects where I needed compression for transfered data, I hit some very interesting compression libraries. Also the ByteArray class contains compress method, using zlib algorithm in flash player or multiple algorithms in AIR. At the end I decided to useByteArray.compress() method for encoding vs. PHP gzuncompress for decoding, what works correctly, fast and smooth. Here is a list of 3rd party compression libraries and other good stuff:

  • AS3 Zip: ActionScript 3 based library for reading and writing zip files
  • FZip: FZip is an Actionscript 3 class library to load, modify and create standard ZIP archives.
  • ASZip: ActionScript 3 library to generate ZIP files
  • LZMA Encoder: AS3 class to compress data using LZMA algorithm.
  • LZMA Decoder: A part of the apparat framework.
  • GZIP: ActionScript GZIP compression library
  • Gzip for HTTPService/URLLoader: Adding Gzip support for Flex/AIR HTTPService/URLLoader
  • airxzip: Zip library for ActionScript3 on AIR

If you know some more, please let me know.

大文件快速分析和查找

有时候有这种需求,需要从一个二进制大文件中查找是否有某种格式的文件,由于大文件是二进制,且非常大,几百M,一般的手工查找方式不太现实了。可以可以通过代码来遍历二进制,查找是否有该文件格式的关键字数据(一般文件头都会有标识,比如dds文件首三个字节是DDS,png、jpg、swf等文件都会有自己的标识头)。

关于搜索算法,我一开始想到的是字符串匹配算法,Google了一下,关于字符串匹配算法有很多种,其中称为Sunday的算法速度非常快,且非常容易理解。关于该算法的具体实现,可以查看本文末尾提供的参考资料,特别是那个PPT,看那个例子非常浅显明了。

字符串匹配算法只能从固定长度内存中匹配,几百M几G的文件不可能一次性全部读取到内存中,所以我使用了分块读取,而在读取下一块文件时,文件指针向后滑动一个窗口,重叠读取,避免这部分内容漏掉匹配。

阅读全文 »»

GLFW 3.x的MAKEFILE脚本(mingw)

下载下来的GLFW需要使用CMAKE编译,但我电脑上只有mingw环境,所以把CMAKE的脚本翻译成MAKEFILE,希望对你有用!


    APP = simple
    OBJS = src/context.o src/init.o src/input.o src/monitor.o src/window.o  \
        src/win32_init.o src/win32_monitor.o src/win32_time.o src/win32_tls.o \
        src/win32_window.o src/winmm_joystick.o src/wgl_context.o \
        examples/simple.o

    INCLUDE = -I. -I./include -I./deps 
    LIB = -L. -lopengl32 -lgdi32 -lglu32

    CFLAGS = -std=gnu99 -DWIN32 -D_GLFW_USE_OPENGL -D_GLFW_WIN32 -D_GLFW_WGL -DUNICODE
    WARNS = -Wno-unknown-pragmas -Wl,--subsystem,console
    LDFLAGS = ${LIB} ${WARNS}

    all: ${APP}.exe

    ${APP}.exe : ${OBJS}
        ${CC} -o $@ ${OBJS} ${LDFLAGS}

    clean :
        $(RM) src/*.o -f
        $(RM) examples/*.o -f

    %.o : %.c ${HEADERS}
        ${CC} ${CFLAGS} ${INCLUDE} -c $< -o $@

da14580驱动全彩RGB LED(内嵌芯片ws2813/ws2812)

类似单总线,但是对时钟要求比较高,需要能精细到0.5微秒的延时,记得传输数据前需要拉低至少50微秒.

    #include <stdio.h>
    #include <stdbool.h>
    #include "datasheet.h"
    #include "gpio.h"

    #define RGB_PORT         GPIO_PORT_0
    #define RGB_PIN          GPIO_PIN_3

    void set_colori(uint32_t color) {
        //author:yoyo(//yoyo.play175.com/p/da14580-ws2813.html)
        int i;
        for(i = 0; i < 24; ++i) {
            if(color & 1) {
                SetWord16(GPIO_BASE + (RGB_PORT << 5) + 2, 1 << RGB_PIN);
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
                __nop();
                __nop();
            } else {
                SetWord16(GPIO_BASE + (RGB_PORT << 5) + 2, 1 << RGB_PIN);
                __nop();
                __nop();
                SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
                __nop();
            }
            color >>= 1;
        }
    }

    void set_color3(uint8_t r, uint8_t g, uint8_t b) {
        set_colori((b << 16) | (r << 8) | g);
    }

    void delay(int ms) {
        unsigned int i = 2400 * ms;
        while(i--) {}
    }

    void system_init(void) {
        SetWord16(CLK_AMBA_REG, 0x00);                 // set clocks (hclk and pclk ) 16MHz
        SetWord16(SET_FREEZE_REG, FRZ_WDOG);           // stop watch dog
        SetBits16(SYS_CTRL_REG, PAD_LATCH_EN, 1);      // open pads
        SetBits16(SYS_CTRL_REG, DEBUGGER_ENABLE, 1);   // open debugger
        SetBits16(PMU_CTRL_REG, PERIPH_SLEEP, 0);      // exit peripheral power down
    }

    int main (void) {
        system_init();

        GPIO_ConfigurePin(RGB_PORT, RGB_PIN, OUTPUT, PID_GPIO, false);
        SetWord16(GPIO_BASE + (RGB_PORT << 5) + 4, 1 << RGB_PIN);
        delay(1);

        set_color3(0xff,0,0);

        while(1);
    }