当前在线人数13506
首页 - 博客首页 - 我的博客 - 文章阅读 [博客首页] [首页]
无聊贴个无聊代码
作者:everid
发表时间:2018-11-05
更新时间:2018-11-05
浏览:119次
评论:4篇
地址:98.
::: 栏目 :::

#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
#include <stdio.h>
#include <string.h>

enum Type{
EGG=0,
SNAKE,
MAN
};

struct Header{
Type type;
}__attribute__((packed));

struct Egg:Header{
char core;
}__attribute__((packed));

struct Snake:Header{
long tail;
}__attribute__((packed));

struct Man:Header{
char name[10];
char sex[10];
}__attribute__((packed));

union Event{
Header h;
Egg e;
Snake s;
Man m;
Event(){}
Event(const Event&event)
{
m = event.m;
}
~Event(){}
};

boost::lockfree::spsc_queue<Event> q{100};

void produce()
{
{
Event event;

Snake snake;
snake.type=SNAKE;
snake.tail=1234567890;

event.s=snake;
q.push(event);
}

{
Event event;

Egg egg;
egg.type=EGG;
egg.core='e';

event.e=egg;
q.push(event);
}


{
Event event;

Man man;
man.type=MAN;
strcpy(man.name, "Jack");
strcpy(man.sex, "male");

event.m=man;
q.push(event);
}
}

void consume()
{
Event event;
while (q.pop(event))
{
if(event.h.type == EGG)
{
Egg e = event.e;
std::cout << "egg: " << e.core << std::endl;
}
else if(event.h.type == SNAKE)
{
Snake s = event.s;
std::cout << "snake: " << s.tail << std::endl;

}
else if(event.h.type == MAN)
{
Man m = event.m;
std::cout << "man: " << m.name << ", " <<
m.sex << std::endl;
}
}
}

int main()
{
std::thread t1{produce};
std::thread t2{consume};
t1.join();
t2.join();
// consume();
}

[上一篇] [下一篇] [发表评论] [写信问候] [收藏] [举报] 
 
共有4条评论
1   [everid 于 2018-11-16 00:32:56 提到] [FROM: 98.]
http://www.kegel.com/dkftpbench/nonblocking.html
 
2   [everid 于 2018-11-16 00:30:23 提到] [FROM: 98.]
https://github.com/Qiurenderen/gitReal
 
3   [everid 于 2018-11-16 00:03:11 提到] [FROM: 98.]
https://www.jianshu.com/p/bec51e6f9294
 
4   [everid 于 2018-11-05 23:41:26 提到] [FROM: 98.]
Header* h = reinterpret_cast<Header*>(&event);
 
用户名: 密码:
发表评论
评论:
[返回顶部] [刷新]  [给everid写信]  [我的博客首页] [博客首页] [BBS 未名空间站]
 
Site Map - Contact Us - Terms and Conditions - Privacy Policy

版权所有BBS 未名空间站(mitbbs.com) since 1996