上次的修改一下
链接在此:[无锁队列一]
//c_buffer.h
class c_data
{
private:
s_memory v_root = NULL;
s_memory *v_r = NULL;
s_memory *v_w = NULL;
int v_num = 0;
public:
~c_data()
{
s_memory_block * pos = v_root.v_next;
int num = 0;
while (1) {
int flag = pos->flag;
s_memory_block * tmp = pos->v_next;
delete pos;
pos = tmp;
if (++num == v_num) //cycle must be out
break;
}
}
int func_mem_create(int pagesize, int num = 200)
{
s_memory_block * pos = NULL;
s_memory_block * smb = new s_memory_block(pagesize);
v_root.v_next = smb; //the first
smb->flag = 0;
pos = smb;
for (int i = 1; i < num; i++)
{
smb = new s_memory(pagesize);
smb->flag = i;
pos->v_next = smb;
pos = smb;
}
pos->v_next = v_root.v_next;
v_w = &v_root;
v_r = &v_root;
v_num = num;
return 0;
}
s_memory *func_mem_write_get()
{
while (1)
{
char m = v_w->v_next->v_lock.load(std::memory_order_relaxed);
if(m != en_read)
{
size_t pack_num = v_w->v_pack_num;
v_w = v_w->v_next;
v_w->v_lock.store(en_write, std::memory_order_relaxed);
//v_w->v_lock = en_write;
v_w->v_pack_num = pack_num +1;
return v_w;
}
else {
//give up the left cpu time let read to do what to do
std::this_thread::yield();
}
}
}
//this function will end the memory barriar
void func_mem_write_fin(s_memory * w)
{
w->v_lock = en_middle;
}
s_memory * func_mem_read_get()
{
char m = v_r->v_next->v_lock.load(std::memory_order_relaxed);
if (m == en_middle)//en_middle can read , en_write can not read
{
v_r->v_next->v_lock = en_read;
v_r = v_r->v_next;
return v_r;
}
return NULL;
}
void func_mem_read_fin(s_memory *r)
{
r->v_lock.store(en_middle, std::memory_order_relaxed);
}
};
测试:
#include "c_buffer.h"
#include <iostream>
using namespace std;
int main()
{
c_data mdata;
mdata.func_mem_create(M_MTU);
#if 1
std::thread thd([&mdata]() {
for (;;)
{
s_memory * r = mdata.func_mem_read_get();
//this is the exit condition
if (r && r->v_pack_num == 999)
{
cout << "over" << endl;
break;
}
//cout << r->v_mem << endl;
if (r != NULL)
{
cout << this_thread::get_id() << r->v_mem << endl;
mdata.func_mem_read_fin(r);
}
else
this_thread::yield();
}
});
#endif
for (int i = 0; i < 1000; i++)
{
s_memory * w = mdata.func_mem_write_get();
cout << "the old number is " << w->v_pack_num << endl;
cout << "the flag is " << w->flag << endl;
sprintf((char*)w->v_mem, "this is number of %d", i);
w->v_size_use =(int)strlen((const char*)w->v_mem);
mdata.func_mem_write_fin(w);
}
if (thd.joinable())
thd.join();
cout << "all end\n";
return 0;
}
成功测试,结果正常
未完待续