C++ 危险指针队列应用程序中的内存使用
我最近测试了具有危险指针应用程序的队列,如下所示: 我注意到RES在我自己的测试ap中上升得很快 内存错误:[30770]:GSlice:分配120字节失败(对齐:128):无法分配内存 发生在最后,ap有5个线程要排队,5个线程要出列,然后 每个线程发送到5个套接字服务器,如下所示:C++ 危险指针队列应用程序中的内存使用,c++,c,multithreading,memory,C++,C,Multithreading,Memory,我最近测试了具有危险指针应用程序的队列,如下所示: 我注意到RES在我自己的测试ap中上升得很快 内存错误:[30770]:GSlice:分配120字节失败(对齐:128):无法分配内存 发生在最后,ap有5个线程要排队,5个线程要出列,然后 每个线程发送到5个套接字服务器,如下所示: #include "lf-queue.h" typedef struct People_ { int age ; char name[16] ; double income ; } P
#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
}
#包括“lf queue.h”
typedef结构人_
{
智力年龄;
字符名[16];
双倍收入;
}人,;
#定义NUM 20000000
LfQueue*q;
int GlbCnt,GlbRow;
int-iConn[5];
int main()
{
q=lf_queue_new();
g_thread_init(NULL);
如果(DoConnect()!=1)
{
printf(“connect server.exe中的错误,端口=55665567556855695570!\n”);
出口(0);
}
pthread_t tid1[5],tid2[5];
int icnt1[5]={0,1,2,3,4};
int icnt2[5]={0,1,2,3,4};
无效*功能1(无效*);
无效*功能2(无效*);
对于(int idx=0;idxname,“%s%08d”,“NAME”,p->age);
p->收入=p->年龄*1.0;
lf_queue_enqueue(q,p);
}
打破
}//而
}
void*func2(void*inum)
{
int-ithread=(长)(int)inum;
pthread_detach(pthread_self());
人民*p;
而(1){
usleep(1);
p=(人*)如果排队(q);;
如果(p){
字符行[128]={0};
sprintf(第行,“%010d |%s |%010.0f |”,p->age,p->name,p->income);
如果(发送(iConn[ithread],第35行,MSG_NOSIGNAL)