怎样设置数组元素的变化作为事件触发机制的事件

cnblackmamba 2014-03-19 09:55:18
现在程序在内存中开辟一个数组,存放实时采集到的数据,一个线程相当于专门采集数据,放入数组中。
现在想实现,每隔20ms发送一次该数组中变化的数据
其实也就是基于事件驱动来传输变化数据

可是不知道,这个事件驱动(数组元素变化)应该怎么实现,难道是每隔20ms扫描一次所有元素?然后跟之前的进行对比么?

求帮忙,谢谢
...全文
239 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-03-20
  • 打赏
  • 举报
回复
如果是固件设计,基本上就是定时器或者直接用中断处理。 如果是硬件就是触发器,比较器了,可能还需要锁存器了。 如果是软件,定时器,线程,锁等等。 不知道楼主设计的是偏向那个方面的
derekrose 2014-03-20
  • 打赏
  • 举报
回复
设置一个bool不就行了
赵4老师 2014-03-20
  • 打赏
  • 举报
回复
参考C:\Program Files\Microsoft Visual Studio\VC98\CRT\SRC\Intel\MEMCMP.ASM
        page    ,132
        title   memcmp - compare to blocks of memory
;***
;memcmp.asm - compare two blocks of memory
;
;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
;
;Purpose:
;       defines memcmp() - compare two memory blocks lexically and
;       find their order.
;
;*******************************************************************************

        .xlist
        include cruntime.inc
        .list

page
;***
;int memcmp(buf1, buf2, count) - compare memory for lexical order
;
;Purpose:
;       Compares count bytes of memory starting at buf1 and buf2
;       and find if equal or which one is first in lexical order.
;
;       Algorithm:
;       int
;       memcmp (buf1, buf2, count)
;               char *buf1, *buf2;
;               unsigned count;
;       {
;               if (!count)
;                       return(0);
;               while (--count && *buf1 == *buf2)
;                       {
;                       buf1++;
;                       buf2++;
;                       }
;               return(*buf1 - *buf2);
;       }
;
;Entry:
;       char *buf1, *buf2 - pointers to memory sections to compare
;       unsigned count - length of sections to compare
;
;Exit:
;       returns -1 if buf1 < buf2
;       returns  0 if buf1 == buf2
;       returns +1 if buf1 > buf2
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************

        CODESEG

        public  memcmp
memcmp  proc

        .FPO    ( 0, 3, 0, 0, 0, 0 )

        mov     eax,[esp+0ch]   ; eax = counter
        test    eax,eax         ; test if counter is zero
        jz      short retnull   ; return 0

        mov     edx,[esp+4]     ; edx = buf1
        push    esi
        push    edi
        mov     esi,edx         ; esi = buf1
        mov     edi,[esp+10h]   ; edi = buf2

; Check for dword (32 bit) alignment
        or      edx,edi
        and     edx,3           ; edx=0 iff buf1 are buf2 are aligned
        jz      short dwords

; Strings are not aligned. If the caller knows the strings (buf1 and buf2) are
; different, the function may be called with length like -1. The difference
; may be found in the last dword of aligned string, and because the other
; string is misaligned it may cause page fault. So, to be safe. the comparison
; must be done byte by byte.
        test    eax,1
        jz      short main_loop

        mov     cl,[esi]
        cmp     cl,[edi]
        jne     short not_equal
        inc     esi
        inc     edi
        dec     eax
        jz      short done      ; eax is already 0

main_loop:
        mov     cl,[esi]
        mov     dl,[edi]
        cmp     cl,dl
        jne     short not_equal

        mov     cl,[esi+1]
        mov     dl,[edi+1]
        cmp     cl,dl
        jne     short not_equal

        add     edi,2
        add     esi,2

        sub     eax,2
        jnz     short main_loop
done:
        pop     edi
        pop     esi
retnull:
        ret                     ; _cdecl return


dwords:
        mov     ecx,eax
        and     eax,3           ; eax= counter for tail loop

        shr     ecx,2
        jz      short tail_loop_start
                                ; counter was >=4 so may check one dword
        rep     cmpsd

        jz      short tail_loop_start

; in last dword was difference
        mov     ecx,[esi-4]     ; load last dword from buf1 to ecx
        mov     edx,[edi-4]     ; load last dword from buf2 to edx
        cmp     cl,dl           ; test first bytes
        jne     short difference_in_tail
        cmp     ch,dh           ; test seconds bytes
        jne     short difference_in_tail
        shr     ecx,10h
        shr     edx,10h
        cmp     cl,dl           ; test third bytes
        jne     short difference_in_tail
        cmp     ch,dh           ; they are different, but each one is bigger?
;       jmp     short difference_in_tail

difference_in_tail:
        mov     eax,0
                                ; buf1 < buf2 buf1 > buf2
not_equal:
        sbb     eax,eax         ; AX=-1, CY=1 AX=0, CY=0
        pop     edi             ; counter
        sbb     eax,-1          ; AX=-1 AX=1
        pop     esi
        ret                     ; _cdecl return

; in tail loop we test last three bytes (esi and edi are aligned on dword
; boundary)
tail_loop_start:

        test    eax,eax         ; eax is counter%4 (number of bytes for tail
                                ; loop)
        jz      short done      ; taken if there is no tail bytes
        mov     edx,[esi]       ; load dword from buf1
        mov     ecx,[edi]       ; load dword from buf2
        cmp     dl,cl           ; test first bytes
        jne     short difference_in_tail
        dec     eax             ; counter--
        jz      short tail_done
        cmp     dh,ch           ; test second bytes
        jne     short difference_in_tail
        dec     eax             ; counter--
        jz      short tail_done
        and     ecx,00ff0000h   ; test third bytes
        and     edx,00ff0000h
        cmp     edx,ecx
        jne     short difference_in_tail
        dec     eax
tail_done:
        pop     edi
        pop     esi
        ret                     ; _cdecl return

memcmp  endp
        end

赵4老师 2014-03-20
  • 打赏
  • 举报
回复
参考X86汇编指令 CMPS 串比较.( CMPSB 比较字符. CMPSW 比较字. ) ?
ldxab 2014-03-19
  • 打赏
  • 举报
回复
sendmessage函数,产生消息
赵4老师 2014-03-19
  • 打赏
  • 举报
回复
无论如何都要调用memcmp函数吧。 无论如何都要加锁吧。 无论如何都要写日志吧。 仅供参考:
//循环向a函数每次发送200个字节长度(这个是固定的)的buffer,
//a函数中需要将循环传进来的buffer,组成240字节(也是固定的)的新buffer进行处理,
//在处理的时候每次从新buffer中取两个字节打印
#ifdef WIN32
    #pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <process.h>
    #include <io.h>
    #define  MYVOID             void
    #define  vsnprintf          _vsnprintf
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  MYVOID             void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
    Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
    usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
#define ASIZE    200
#define BSIZE    240
#define CSIZE      2
char Abuf[ASIZE];
char Cbuf[CSIZE];
CRITICAL_SECTION cs_HEX ;
CRITICAL_SECTION cs_BBB ;
struct FIFO_BUFFER {
    int  head;
    int  tail;
    int  size;
    char data[BSIZE];
} BBB;
int No_Loop=0;
void HexDump(int cn,char *buf,int len) {
    int i,j,k;
    char binstr[80];

    Lock(&cs_HEX);
    for (i=0;i<len;i++) {
        if (0==(i%16)) {
            sprintf(binstr,"%03d %04x -",cn,i);
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        } else if (15==(i%16)) {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
            sprintf(binstr,"%s  ",binstr);
            for (j=i-15;j<=i;j++) {
                sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
            }
            Log("%s\n",binstr);
        } else {
            sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
        }
    }
    if (0!=(i%16)) {
        k=16-(i%16);
        for (j=0;j<k;j++) {
            sprintf(binstr,"%s   ",binstr);
        }
        sprintf(binstr,"%s  ",binstr);
        k=16-k;
        for (j=i-k;j<i;j++) {
            sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
        }
        Log("%s\n",binstr);
    }
    Unlock(&cs_HEX);
}
int GetFromRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;

    lent=0;
    Lock(cs);
    if (fbuf->size>=len) {
        lent=len;
        if (fbuf->head+lent>BSIZE) {
            len1=BSIZE-fbuf->head;
            memcpy(buf     ,fbuf->data+fbuf->head,len1);
            len2=lent-len1;
            memcpy(buf+len1,fbuf->data           ,len2);
            fbuf->head=len2;
        } else {
            memcpy(buf     ,fbuf->data+fbuf->head,lent);
            fbuf->head+=lent;
        }
        fbuf->size-=lent;
    }
    Unlock(cs);
    return lent;
}
MYVOID thdB(void *pcn) {
    char        *recv_buf;
    int          recv_nbytes;
    int          cn;
    int          wc;
    int          pb;

    cn=(int)pcn;
    Log("%03d thdB              thread begin...\n",cn);
    while (1) {
        sleep_ms(10);
        recv_buf=(char *)Cbuf;
        recv_nbytes=CSIZE;
        wc=0;
        while (1) {
            pb=GetFromRBuf(cn,&cs_BBB,&BBB,recv_buf,recv_nbytes);
            if (pb) {
                Log("%03d recv %d bytes\n",cn,pb);
                HexDump(cn,recv_buf,pb);
                sleep_ms(1);
            } else {
                sleep_ms(1000);
            }
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
#ifndef WIN32
    pthread_exit(NULL);
#endif
}
int PutToRBuf(int cn,CRITICAL_SECTION *cs,FIFO_BUFFER *fbuf,char *buf,int len) {
    int lent,len1,len2;

    Lock(cs);
    lent=len;
    if (fbuf->size+lent>BSIZE) {
        lent=BSIZE-fbuf->size;
    }
    if (fbuf->tail+lent>BSIZE) {
        len1=BSIZE-fbuf->tail;
        memcpy(fbuf->data+fbuf->tail,buf     ,len1);
        len2=lent-len1;
        memcpy(fbuf->data           ,buf+len1,len2);
        fbuf->tail=len2;
    } else {
        memcpy(fbuf->data+fbuf->tail,buf     ,lent);
        fbuf->tail+=lent;
    }
    fbuf->size+=lent;
    Unlock(cs);
    return lent;
}
MYVOID thdA(void *pcn) {
    char        *send_buf;
    int          send_nbytes;
    int          cn;
    int          wc;
    int           a;
    int          pa;

    cn=(int)pcn;
    Log("%03d thdA              thread begin...\n",cn);
    a=0;
    while (1) {
        sleep_ms(100);
        memset(Abuf,a,ASIZE);
        a=(a+1)%256;
        if (16==a) {No_Loop=1;break;}//去掉这句可以让程序一直循环直到按Ctrl+C或Ctrl+Break或当前目录下存在文件No_Loop
        send_buf=(char *)Abuf;
        send_nbytes=ASIZE;
        Log("%03d sending %d bytes\n",cn,send_nbytes);
        HexDump(cn,send_buf,send_nbytes);
        wc=0;
        while (1) {
            pa=PutToRBuf(cn,&cs_BBB,&BBB,send_buf,send_nbytes);
            Log("%03d sent %d bytes\n",cn,pa);
            HexDump(cn,send_buf,pa);
            send_buf+=pa;
            send_nbytes-=pa;
            if (send_nbytes<=0) break;//
            sleep_ms(1000);
            if (No_Loop) break;//
            wc++;
            if (wc>3600) Log("%03d %d==wc>3600!\n",cn,wc);
        }
        if (No_Loop) break;//
    }
#ifndef WIN32
    pthread_exit(NULL);
#endif
}
int main() {
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
    InitializeCriticalSection(&cs_HEX );
    InitializeCriticalSection(&cs_BBB );
#else
    pthread_t threads[2];
    int threadsN;
    int rc;
    pthread_mutex_init(&cs_log,NULL);
    pthread_mutex_init(&cs_HEX,NULL);
    pthread_mutex_init(&cs_BBB,NULL);
#endif
    Log("Start===========================================================\n");

    BBB.head=0;
    BBB.tail=0;
    BBB.size=0;

#ifdef WIN32
    _beginthread((void(__cdecl *)(void *))thdA,0,(void *)1);
    _beginthread((void(__cdecl *)(void *))thdB,0,(void *)2);
#else
    threadsN=0;
    rc=pthread_create(&(threads[threadsN++]),NULL,thdA,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
    rc=pthread_create(&(threads[threadsN++]),NULL,thdB,(void *)2);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif

    if (!access("No_Loop",0)) {
        remove("No_Loop");
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    while (1) {
        sleep_ms(1000);
        if (No_Loop) break;//
        if (!access("No_Loop",0)) {
            No_Loop=1;
        }
    }
    sleep_ms(3000);
    Log("End=============================================================\n");
#ifdef WIN32
    DeleteCriticalSection(&cs_BBB );
    DeleteCriticalSection(&cs_HEX );
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_BBB);
    pthread_mutex_destroy(&cs_HEX);
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
飞天御剑流 2014-03-19
  • 打赏
  • 举报
回复
引用 6 楼 cnblackmamba 的回复:
我想再细化一下问题,其实,数组元素的更新,不是全部更新的 当采集的数据往数组中对应的下标存放的时候,如果与原来的值不同的时候,就发送给客户端。如果与原来的值不同的时候,就不用发送。 事件驱动:当数组中某个元素值变了,就把它发送给客户端。 只是这个事件驱动应该怎么写啊,不知道应该怎么来实现,是我过一段时间扫描一下数组看看它变了哪些,然后发送出去?
写者写入的时候,把数组的下标写入一个set中,另一个线程每隔20ms读取这个set,发送相应下标的元素。 要加锁。
mangoalx 2014-03-19
  • 打赏
  • 举报
回复
引用 8 楼 yqpkilo 的回复:
另一个线程改变数组数据的时候,难道不能在其中加入PostMessage吗
这是正道。写入的时候判断要容易得多
JiMoKuangXiangQu 2014-03-19
  • 打赏
  • 举报
回复
或者使用OS机制(互斥等等)实现生产者/消费者模式。
JiMoKuangXiangQu 2014-03-19
  • 打赏
  • 举报
回复
可以试试ring buffer方式。
kiloyqp 2014-03-19
  • 打赏
  • 举报
回复
另一个线程改变数组数据的时候,难道不能在其中加入PostMessage吗
碼上道 2014-03-19
  • 打赏
  • 举报
回复
你的数组是多大,如果不是很大,什么办法估计差不多,直接扫描就可以了。
cnblackmamba 2014-03-19
  • 打赏
  • 举报
回复
我想再细化一下问题,其实,数组元素的更新,不是全部更新的 当采集的数据往数组中对应的下标存放的时候,如果与原来的值不同的时候,就发送给客户端。如果与原来的值不同的时候,就不用发送。 事件驱动:当数组中某个元素值变了,就把它发送给客户端。 只是这个事件驱动应该怎么写啊,不知道应该怎么来实现,是我过一段时间扫描一下数组看看它变了哪些,然后发送出去?
turing-complete 2014-03-19
  • 打赏
  • 举报
回复
使用条件变量,如果你编译器允许的话,可以使用std::condition_variable

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧