15,440
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include "time_count.h"
#include "stdint.h"
const int count_per_thread_push = 100000000;
const float CPU_MHZ = 1596; //use cat /proc/cpuinfo get the value
const float CPU_tick_count_per_second = CPU_MHZ*1000*1000;
struct lock_free_queue //我把实现部分去掉了,框架供参考,其中打印部分帮助调试,确定延迟。正式比拼可以注释掉。
{
void push(unsigned long long pop_time)
{
};
bool pop()
{
/*if(now%(1000000) == 0)
{
printf("task get:%u,task write:%u,latency:%lf\n",m_head[now],rdtsc(),(rdtsc()-m_head[now])/CPU_tick_count_per_second);
}*/
};
lock_free_queue()
{
};
~lock_free_queue()
{
}
};
void* pop(void* queue)
{
lock_free_queue* lfq = (lock_free_queue*)queue;
do{
}while(lfq->pop());
};
void* push(void* queue)
{
lock_free_queue* lfq = (lock_free_queue*)queue;
for(int i=0;i<count_per_thread_push/4;++i)
{
unsigned long long now = rdtsc();
lfq->push(now);
lfq->push(now);
lfq->push(now);
lfq->push(now);
}
};
void* push_end(void* queue)
{
lock_free_queue* lfq = (lock_free_queue*)queue;
for(int i=0;i<1000;++i)
{
lfq->push(0);
}
}
int main(void)
{
pthread_t* thread_pop = (pthread_t*) malloc(10*sizeof( pthread_t));
pthread_t* thread_push = (pthread_t*) malloc(10*sizeof( pthread_t));
pthread_t* thread_push_end = (pthread_t*) malloc(sizeof( pthread_t));
lock_free_queue lfq;
for(int i=0;i<10;++i)
{
pthread_create(&thread_push[i],NULL,push,&lfq);
}
for(int i=0;i<10;++i)
{
pthread_create(&thread_pop[i],NULL,pop,&lfq);
}
for(int i=0;i<10;++i) //make push end
{
pthread_join(thread_push[i],NULL);
}
pthread_create(thread_push_end,NULL,push_end,&lfq); //push end signal
for(int i=0;i<10;++i) //wait pop quit
{
pthread_join(thread_pop[i],NULL);
}
if( NULL != thread_pop )
{
free(thread_pop);
thread_pop = NULL;
}
if( NULL != thread_pop )
{
free(thread_push);
thread_push = NULL;
}
if( NULL != thread_push_end )
{
free( thread_push_end );
thread_push_end = NULL;
}
}