-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from sonia-auv/develop
Develop
- Loading branch information
Showing
7 changed files
with
376 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#********** Driver Parameters ************ | ||
|
||
/proc_underwater_com/settings/number_mission : 16 | ||
/proc_underwater_com/settings/number_mission : 11 | ||
/proc_underwater_com/settings/delay_ack : 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#ifndef INTERFACE_RS485_SHAREDQUEUE_H | ||
#define INTERFACE_RS485_SHAREDQUEUE_H | ||
|
||
#include <queue> | ||
#include <mutex> | ||
#include <condition_variable> | ||
|
||
template <typename T> | ||
class SharedQueue | ||
{ | ||
public: | ||
SharedQueue(); | ||
~SharedQueue(); | ||
|
||
T& front(); | ||
void pop_front(); | ||
|
||
T get_n_pop_front(); | ||
|
||
void push_back(const T& item); | ||
void push_back(T&& item); | ||
|
||
|
||
unsigned long size(); | ||
bool empty(); | ||
|
||
private: | ||
std::deque<T> queue_; | ||
std::mutex mutex_; | ||
std::condition_variable cond_; | ||
}; | ||
|
||
template <typename T> | ||
SharedQueue<T>::SharedQueue(){} | ||
|
||
template <typename T> | ||
SharedQueue<T>::~SharedQueue(){} | ||
|
||
template <typename T> | ||
T& SharedQueue<T>::front() | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
while (queue_.empty()) | ||
{ | ||
cond_.wait(mlock); | ||
} | ||
return queue_.front(); | ||
} | ||
|
||
template <typename T> | ||
void SharedQueue<T>::pop_front() | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
while (queue_.empty()) | ||
{ | ||
cond_.wait(mlock); | ||
} | ||
queue_.pop_front(); | ||
} | ||
|
||
template <typename T> | ||
T SharedQueue<T>::get_n_pop_front() | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
while (queue_.empty()) | ||
{ | ||
cond_.wait(mlock); | ||
} | ||
T temp = queue_.front(); | ||
queue_.pop_front(); | ||
mlock.unlock(); | ||
cond_.notify_one(); | ||
return temp; | ||
} | ||
|
||
template <typename T> | ||
void SharedQueue<T>::push_back(const T& item) | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
queue_.push_back(item); | ||
mlock.unlock(); // unlock before notificiation to minimize mutex con | ||
cond_.notify_one(); // notify one waiting thread | ||
|
||
} | ||
|
||
template <typename T> | ||
void SharedQueue<T>::push_back(T&& item) | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
queue_.push_back(std::move(item)); | ||
mlock.unlock(); // unlock before notificiation to minimize mutex con | ||
cond_.notify_one(); // notify one waiting thread | ||
|
||
} | ||
|
||
template <typename T> | ||
unsigned long SharedQueue<T>::size() | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
unsigned long size = queue_.size(); | ||
mlock.unlock(); | ||
cond_.notify_one(); | ||
return size; | ||
} | ||
|
||
template <typename T> | ||
bool SharedQueue<T>::empty() | ||
{ | ||
std::unique_lock<std::mutex> mlock(mutex_); | ||
bool empty = queue_.empty(); | ||
mlock.unlock(); | ||
cond_.notify_one(); | ||
return empty; | ||
} | ||
|
||
#endif //INTERFACE_RS485_SHAREDQUEUE_H |
Oops, something went wrong.