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