CommandQueue: read/write 加锁保护
This commit is contained in:
parent
06a3cf0668
commit
ce873f5339
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "command_queue.h"
|
#include "command_queue.h"
|
||||||
|
#include "base.h"
|
||||||
|
|
||||||
CommandQueue::CommandQueue()
|
CommandQueue::CommandQueue()
|
||||||
{
|
{
|
||||||
|
@ -32,6 +33,8 @@ CommandQueue::CommandQueue()
|
||||||
|
|
||||||
bool CommandQueue::write(const char *command)
|
bool CommandQueue::write(const char *command)
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(mutex);
|
||||||
|
|
||||||
if (strlen(commands[writeIndex]) != 0) {
|
if (strlen(commands[writeIndex]) != 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -51,6 +54,8 @@ bool CommandQueue::write(const char *command)
|
||||||
|
|
||||||
bool CommandQueue::read(char *dest)
|
bool CommandQueue::read(char *dest)
|
||||||
{
|
{
|
||||||
|
std::unique_lock<std::mutex> lk(mutex);
|
||||||
|
|
||||||
if (readIndex == -1) {
|
if (readIndex == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
#ifndef COMMAND_QUEUE_H
|
#ifndef COMMAND_QUEUE_H
|
||||||
#define COMMAND_QUEUE_H
|
#define COMMAND_QUEUE_H
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
class CommandQueue
|
class CommandQueue
|
||||||
{
|
{
|
||||||
enum
|
enum
|
||||||
|
@ -31,6 +33,8 @@ class CommandQueue
|
||||||
char commands[MAX_COMMAND_COUNT][COMMAND_LENGTH];
|
char commands[MAX_COMMAND_COUNT][COMMAND_LENGTH];
|
||||||
int readIndex, writeIndex;
|
int readIndex, writeIndex;
|
||||||
|
|
||||||
|
std::mutex mutex;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CommandQueue();
|
CommandQueue();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue