admin 管理员组

文章数量: 887053


2024年1月18日发(作者:objective findings)

bool IsEmpty() { return tail == head; } bool Push(DataType data) { if (IsFull()) return false; datas[tail] = data; tail = (tail + 1) % Length; return true; } bool Pop(DataType& data) { if (IsEmpty()) return false; data = datas[head]; head = (head + 1) % Length; return true; } int Size() { if (tail >= head) return tail - head; else return tail + Length - head; }};clock_t start = 0;int b = 0;mutex mt;bool isFinish = false;RingBuffer ringbuffer;void WriteThread(){ for (int index = 0; index < ARRAY_MAX_SIZE; ++index) { //(); if (!(index)) { index = index - 1; } //(); } isFinish = true; clock_t end = clock(); printf("Write DataQueue Num = %dn ", ()); printf("time = %f b = %dn ", (double)(end - start) / CLOCKS_PER_SEC, b);}void ReadThread(){ while (1) { if (isFinish && y()) {

templateclass RingBuffer{private: DataType datas[Length]; int head = 0; int tail = 0;public: bool IsFull() { return (tail + 1) % Length == head; } bool IsEmpty() { return tail == head; } bool Push(DataType data) { if (IsFull()) return false; datas[tail] = data; tail = (tail + 1) % Length; return true; } bool Pop(DataType& data) { if (IsEmpty()) return false; data = datas[head]; head = (head + 1) % Length; return true; } int Size() { if (tail >= head) return tail - head; else return tail + Length - head; }};单写线程和单读线程的ringbuffer#include #include#include#include#include#define ARRAY_MAX_SIZE 4000000using namespace std;templateclass RingBuffer{

{private: DataType datas[Length]; int head = 0; int tail = 0;public: bool IsFull() { return (tail + 1) % Length == head; } bool IsEmpty() { return tail == head; } bool Push(DataType data) { if (IsFull()) return false; datas[tail] = data; tail = (tail + 1) % Length; return true; } bool Pop(DataType& data) { if (IsEmpty()) return false; data = datas[head]; head = (head + 1) % Length; return true; } int Size() { if (tail >= head) return tail - head; else return tail + Length - head; }};clock_t start = 0;int b = 0;mutex mt;bool isFinish = false;RingBuffer ringbuffer;void WriteThread(){ for (int index = 0; index < ARRAY_MAX_SIZE; ++index) { if (!(index)) { index = index - 1; } } isFinish = true; clock_t end = clock();


本文标签: 线程