危险指针队列应用程序中的内存使用

时间:2015-01-15 08:50:02

标签: c++ c multithreading memory

我最近测试了带有危险指针应用程序的队列,如下所示:

https://github.com/chergert/dukes_of_hazard

我注意到RES在我自己的测试ap中提升得非常快,

MEMORY-ERROR :[30770]:GSlice:无法分配120个字节(对齐方式:128):无法分配内存

发生在最后,ap有5个线程排队,5个线程出队然后 发送到每个线程的5个套接字服务器,如下所示:

#include "lf-queue.h"

typedef struct People_
{
    int age ;
    char name[16] ;
    double income ;
} People ;

#define NUM 20000000

LfQueue *q ;
int    GlbCnt,GlbRow ;
int iConn[5] ;

int main()
{
    q = lf_queue_new();
    g_thread_init(NULL);

    if(DoConnect() != 1)
    {
        printf("Error in connect server .exe , port=5566,5567,5568,5569,5570 ! \n") ;
        exit(0) ;
    }

    pthread_t tid1[5],tid2[5];
    int icnt1[5] = {0,1,2,3,4} ;
    int icnt2[5] = {0,1,2,3,4} ;
    void    *func1(void *);
    void    *func2(void *);
    for(int idx=0;idx<5;idx++)
    {
        pthread_create(&tid1[idx], NULL, &func1 , (void *) icnt1[idx] );
        pthread_create(&tid2[idx], NULL, &func2 , (void *) icnt2[idx] );
    }
    while(1){
        printf("Done(%d)\n",GlbCnt) ;
        sleep(1) ;
    }
} //main

void    *func1(void * inum)
{
    int ithread = (long)(int) inum ;
    pthread_detach(pthread_self());
    People* p ;

    while(1){
        for(int idx=0;idx<NUM;idx++){
            usleep(1) ;
            p = (People *) malloc( sizeof(People) ) ;
            p->age = __sync_add_and_fetch(&GlbRow,1)   ;
            sprintf(p->name,"%s%08d","NAME",p->age ) ;
            p->income = p->age * 1.0 ;

            lf_queue_enqueue(q,p) ;
        }
        break ;
    } //while
}

void    *func2(void * inum)
{
    int ithread = (long)(int) inum ;
    pthread_detach(pthread_self());
    People* p ;

    while(1){
        usleep(1) ;
        p = (People *) lf_queue_dequeue(q) ;;
        if(p){
            char line[128]={0} ;
            sprintf(line,"%010d|%s|%010.0f|",p->age,p->name,p->income) ;

            if (send(iConn[ithread],line,35,MSG_NOSIGNAL)<0){
                printf("send error \n");
                exit( 0 ) ;
            }
            __sync_add_and_fetch(&GlbCnt,1) ;
            free(p) ;
        }
    } //while
}

0 个答案:

没有答案
相关问题