Removed Multithreading, simplifying the server
Networking and multithreading working at the same time is really fucking hard. It's better to just have the one thread, and not worry about speed at this stage.
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
#ifndef THREADSAFEQUEUE_HPP_
|
||||
#define THREADSAFEQUEUE_HPP_
|
||||
|
||||
#include "SDL/SDL_thread.h"
|
||||
|
||||
#include <deque>
|
||||
#include <stdexcept>
|
||||
|
||||
/* This container is a thread safe reimplementation of std::queue.
|
||||
*/
|
||||
|
||||
template<typename T, class Container = std::deque<T>>
|
||||
class ThreadSafeQueue {
|
||||
public:
|
||||
ThreadSafeQueue() {
|
||||
lock = SDL_CreateSemaphore(1);
|
||||
if (!lock) {
|
||||
throw(std::runtime_error("Failed to create ThreadSafeQueue::lock"));
|
||||
}
|
||||
}
|
||||
|
||||
~ThreadSafeQueue() {
|
||||
SDL_DestroySemaphore(lock);
|
||||
}
|
||||
|
||||
T PushBack(T t) {
|
||||
SDL_SemWait(lock);
|
||||
container.push_back(t);
|
||||
SDL_SemPost(lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
T PeekFront() {
|
||||
T t;
|
||||
SDL_SemWait(lock);
|
||||
if (container.size() > 0) {
|
||||
t = container[0];
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
T PopFront() {
|
||||
T t;
|
||||
SDL_SemWait(lock);
|
||||
if (container.size() > 0) {
|
||||
t = container[0];
|
||||
container.pop_front();
|
||||
}
|
||||
SDL_SemPost(lock);
|
||||
return t;
|
||||
}
|
||||
|
||||
int Size() {
|
||||
//can't be sure if std::deque::size() is thread safe
|
||||
int ret;
|
||||
SDL_SemWait(lock);
|
||||
ret = container.size();
|
||||
SDL_SemPost(lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
Container container;
|
||||
SDL_sem* lock;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user