I want to implement class in way that different processes can access the same static data:
class Shared()
{
public:
static int GetValue();
static void SetValue(int value);
};
How to do this using shared memory to store internal data. Could anyone help me to do this? Any answer will be appreciated.
Sample code is as shown below, this is a very basic implementation. Class will explain how to create, set/get single value and destroy shared memory. Error checks, notifications etc can be added as policy classes using templates.
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <string.h>
template <key_t KEY, typename T, int COUNT = 1>
class Shm
{
public:
Shm():shm_(0)
{
get();
attach();
}
~Shm()
{
if(shm_ != NULL)
{
shmdt(shm_);
shm_ = 0;
}
}
//Set one element
void SetValue(const T* data, int count = 1)
{
if(sizeof(T)*count > sizeof(T) * COUNT)
{
throw std::runtime_error("Data size greater than shm size");
}
memcpy(shm_, data, sizeof(T)*count);
}
//Get pointer to element
const T* GetValue()
{
T* ptr = new(shm_) T;
return ptr;
}
static void create()
{
if ((shmid_ = shmget(KEY, COUNT*sizeof(T), IPC_CREAT | 0666)) < 0)
{
throw std::runtime_error("Failed create shm");
}
}
static void destroy()
{
get();
if(shmctl(shmid_, IPC_RMID, NULL)<0)
{
perror("shctl");
throw std::runtime_error("Error cannot remove shared memory");
}
shmid_ = -1;
}
private:
static void get()
{
if(shmid_ == -1)
{
if((shmid_ = shmget(KEY, COUNT*sizeof(T), 0666)) < 0)
{
perror("shmget");
throw std::runtime_error("Shared memory not created");
}
}
}
void attach()
{
if ((shm_ = shmat(shmid_, NULL, 0)) == (char *) -1)
{
throw std::runtime_error("Failed attach shm");
}
}
void* shm_;
static int shmid_;
};
template <key_t KEY, typename T, int COUNT>
int Shm<KEY, T, COUNT>::shmid_ = -1;
int main(int argc, char ** argv)
{
if(argc == 2)
{
if(std::string(argv[1]) == "server")
{
int val = 50;
Shm<0x1234, int>::create();
Shm<0x1234, int> shm;
shm.SetValue(&val);
}
else if(std::string(argv[1]) == "client")
{
Shm<0x1234, int> shm;
const int* ptr = shm.GetValue();
std::cout <<"Val = " << *ptr <<std::endl;
Shm<0x1234, int>::destroy();
}
}
else
{
std::cerr<<"Usage shm [server][client]"<<std::endl;
}
return 0;
}
You really don't want to hand-roll a solution for this (specially if it has to be portable), but fortunately boost::interprocess that will give such features and more (e.g. allocators) for shared memory.
Minimal example (untested):
#include <boost/interprocess/managed_shared_memory.hpp>
using namespace boost::interprocess;
void* allocate(size_t bytes)
{
static managed_shared_memory segment(create_only, "MySharedMemory", 65536);
return segment.allocate(bytes);
}
Related
What is the best way to end the lifetime of an object with static storage duration?
Current implementation finds the caller of __run_exit_handlers which then will be used to determine the __exit_funcs.
However this would easily fail since offset to __run_exit_handlers can change easily even in glibc with the same version. Another thing that could be done is to resolve the address of __run_exit_handlers first then use it in finding the caller rather than using a hardcoded call offset.
Current Working Code:
#include <iostream>
#include <fstream>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <execinfo.h>
struct A
{
A(std::string pName)
: mName(pName)
{
std::printf("%s %s\n", __PRETTY_FUNCTION__, mName.c_str());
}
~A()
{
std::printf("%s %s\n", __PRETTY_FUNCTION__, mName.c_str());
}
volatile int i = 0;
std::string mName;
};
A a{"a"};
A b{"b"};
A c{"c"};
class StaticDestroyer
{
public:
StaticDestroyer()
{
std::ifstream maps("/proc/self/maps", std::ios::in);
char line[1024];
uint8_t* magic = nullptr;
while (maps.getline(line, sizeof(line)))
{
char perms[4];
uint8_t *magicbegin, *magicend;
std::string lsv(line);
if (std::string::npos == lsv.find("/libc-",0,6)) continue;
std::sscanf(line, "%lx-%lx %4s", &magicbegin, &magicend, perms);
if (perms[0]==114 && perms[2]==120)
{
magic = findMagic(magicbegin, magicend);
break;
}
}
if (magic==nullptr)
throw std::runtime_error("magic not found!");
mHead = *(HakaishinNode**)magic;
}
bool destroy(void* pTarget)
{
HakaishinNode *current = mHead;
while (nullptr != current)
{
for (size_t i = current->idx-1 ; i>0; i--)
{
const Hakaishin *const f = ¤t->fns[i];
if (4 == f->type && pTarget == f->arg)
{
void (*destruct) (void *arg, int status) = f->fn;
asm ("ror $2*8+1, %0\nxor %%fs:%c2, %0" : "=r" (destruct) : "0" (destruct), "i" (48));
destruct (f->arg, 1);
if (current->idx-1 != i) for (size_t j = i; j < current->idx ; j++) current->fns[j] = current->fns[j+1];
current->idx--;
return true;
}
}
current = current->next;
}
return false;
}
private:
struct Hakaishin
{
long int type;
void (*fn) (void *arg, int status);
void *arg;
void *dso_handle;
};
struct HakaishinNode
{
struct HakaishinNode *next;
size_t idx;
Hakaishin fns[32];
};
uint8_t* findMagic(uint8_t* magicbegin, uint8_t* magicend)
{
const void* const begin = magicbegin;
int32_t ptr;
while ((magicbegin+7) <= magicend)
{
if (magicbegin[0]==0x48 && (magicbegin[1]==0x8b || magicbegin[1]==0x8d))
{
std::memcpy(&ptr, magicbegin+3, sizeof(ptr));
uint8_t* magicp = magicbegin+ptr+7;
if (ptr==0x38a5c1) return magicp;
}
magicbegin++;
}
return nullptr;
}
HakaishinNode* mHead = nullptr;
};
A& getA()
{
static A a{"getA"};
return a;
}
A& getA2()
{
static A a{"getA2"};
return a;
}
int main()
{
std::printf("entering...\n");
StaticDestroyer d;
d.destroy(&a);
d.destroy(&b);
auto& ga = getA();
d.destroy(&ga);
getA2();
std::printf("returning...\n");
}
Output:
A::A(std::string) a
A::A(std::string) b
A::A(std::string) c
entering...
A::~A() a
A::~A() b
A::A(std::string) getA
A::~A() getA
A::A(std::string) getA2
returning...
A::~A() getA2
A::~A() c
Static objects will be destructed with the termination of the program.
If you like to manage the resources don't make it static or use a static pointer. Here you can allocate and de-allocate the corresponding resources. This approach comes very close to a singleton, which is considered to be an anti pattern.
Conclusion:
If you need to manage a resource don't make it static.
The need to mess around with the default behavior of life-time in such a way indicates that you have a design flaw in your application.
So you should either consider restructuring your program to not use globals. Or at least change the way how you handle the globals. So if you really need globals and release them earlier, then switch to unique_ptr:
#include <iostream>
#include <functional>
#include <memory>
struct A
{
A(std::string pName)
: mName(pName)
{
std::printf("%s %s\n", __PRETTY_FUNCTION__, mName.c_str());
}
~A()
{
std::printf("%s %s\n", __PRETTY_FUNCTION__, mName.c_str());
}
volatile int i = 0;
std::string mName;
};
auto a = std::make_unique<A>("a");
auto b = std::make_unique<A>("b");
auto c = std::make_unique<A>("c");
auto& getA()
{
static auto a = std::make_unique<A>("getA");
return a;
}
auto& getA2()
{
static auto a = std::make_unique<A>("getA2");
return a;
}
int main() {
std::printf("entering...\n");
a = nullptr;
b = nullptr;
c = nullptr;
getA();
getA2();
getA() = nullptr;
std::printf("returning...\n");
}
That way you can release the objects managed by the unique_ptr earlier, but they will be released on exit automatically if you don't set them to nullptr manually.
I have implemented a serializer to send data over network. And I have implemented a system that can deserialize primitive data, string, map(string, string), map(string, float), but the error happens with map(string, int) when the deserialized map is used to fetch the value from key. In the debugger I can see that map receive correct value but when I'm trying to get data, I get an error "std::out_of_range at memory location".
Here is my code
#include <stdint.h>
#include <memory>
#include <string>
#include <map>
#include <algorithm>
#define STREAM_ENDIANNESS 0
#define PLATFORM_ENDIANNESS 0
using namespace std;
class OutputMemoryStream
{
void ReallocBuffer(uint32_t inNewLength)
{
mBuffer = static_cast<char*>(std::realloc(mBuffer, inNewLength));
mCapacity = inNewLength;
}
char* mBuffer = nullptr;
uint32_t mHead;
uint32_t mCapacity;
public:
OutputMemoryStream() : mHead(0) { ReallocBuffer(32); }
~OutputMemoryStream()
{
if (mBuffer) { mBuffer = nullptr; }
}
char* GetBufferPtr() const { return mBuffer; }
uint32_t GetLength() const { return mHead; }
void Write(const void* inData, size_t inByteCount)
{
//make sure we have space...
uint32_t resultHead = mHead + static_cast<uint32_t>(inByteCount);
if (resultHead > mCapacity)
{
ReallocBuffer(std::max(mCapacity * 2, resultHead));
}
//copy into buffer at head
std::memcpy(mBuffer + mHead, inData, inByteCount);
//increment head for next write
mHead = resultHead;
}
template< typename T > void Write(T inData)
{
static_assert(std::is_arithmetic< T >::value || std::is_enum< T >::value, "Generic Write only supports primitive data types");
if (STREAM_ENDIANNESS == PLATFORM_ENDIANNESS)
{
Write(&inData, sizeof(inData));
}
else { }
}
template< typename T >
void Write(const std::map< string, T >& inMap)
{
uint32_t elementCount = inMap.size();
Write(elementCount);
for (std::pair<string, T> element : inMap)
{
Write(element.first);
Write(element.second);
}
}
void Write(const std::string& inString)
{
size_t elementCount = inString.size();
Write(elementCount + 1);
Write(inString.data(), (elementCount + 1) * sizeof(char));
}
};
class InputMemoryStream
{
private:
char* mBuffer;
uint32_t mHead;
uint32_t mCapacity;
public:
InputMemoryStream() {}
InputMemoryStream(char* inBuffer, uint32_t inByteCount) : mBuffer(inBuffer), mCapacity(inByteCount), mHead(0) { }
~InputMemoryStream()
{
if (mBuffer) { mBuffer = nullptr; }
}
uint32_t GetRemainingDataSize() const
{
return mCapacity - mHead;
}
void Read(void* outData, uint32_t inByteCount)
{
uint32_t resultHead = mHead + inByteCount;
if (resultHead > mCapacity)
{
//handle error, no data to read!
//...
}
std::memcpy(outData, mBuffer + mHead, inByteCount);
mHead = resultHead;
}
template< typename T > void Read(T& outData)
{
static_assert(std::is_arithmetic< T >::value || std::is_enum< T >::value, "Generic Read only supports primitive data types");
Read(&outData, sizeof(outData));
}
template<typename T1>
void Read(std::map<string, T1> &mapP)
{
size_t elemenCount;
Read(elemenCount);
for (int i = 0; i < elemenCount; i++)
{
string key; T1 value;
Read(key);
Read(value);
std::pair<string, T1> pair(key, value);
mapP.insert(pair);
}
}
void Read(string &outString)
{
size_t strSize;
Read(strSize);
outString.resize(strSize);
for (int i = 0; i < strSize; i++)
{
Read(&outString[i], 1);
}
}
};
class ServerObject
{
OutputMemoryStream outStream;
InputMemoryStream inStream;
map<std::string, int> mapInt;
public:
ServerObject() {};
ServerObject(char* byteArray, int byteCount)
{
InputMemoryStream inStream(byteArray, byteCount);
Deserialize(inStream);
}
~ServerObject() {};
void Serialize()
{
outStream.Write(mapInt);
}
void Deserialize(InputMemoryStream inStream)
{
inStream.Read(mapInt);
}
OutputMemoryStream GetOutStream()
{
return outStream;
}
int GetInt(string key)
{
return mapInt.at(key);
}
void PutInt(string key, int value)
{
mapInt.insert(std::pair<string, int>(key, value));
}
};
int main()
{
ServerObject * so = new ServerObject();
so->PutInt("test", 10);
so->Serialize();
ServerObject * so1 = new ServerObject(so->GetOutStream().GetBufferPtr(), so->GetOutStream().GetLength());
int i = so1->GetInt("test");
system("pause>NULL");
return 0;
}
Your void Write(const std::string& inString) function of OutputMemoryStream should not store additional byte of buffer for null terminator because std::string will not contain null terminator but if you use c_str(), a null terminator will be included in the return from this method. Don't get confused with the internal structure of the memory. std::string stores the length of the string in its member variable so there is no need of null terminator. The function should be as shown below.
void Write(const std::string& inString)
{
size_t elementCount = inString.size();
Write(elementCount);
Write(inString.data(), elementCount * sizeof(char));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So im creating an engine from scrath(learnign purposes).
And when i test my TArray i get an Execption thrown in my Memory.h file. This happens only when im trying to test the TArray
Here is my code for TArray.h
#pragma once
#include "Memory/DynamicLinearStackAllocator.h"
template <typename T, typename Allocator = DynamicLinearStackAllocator>
class TArray
{
private:
Allocator m_Allocator;
uint32 m_ElementCount;
public:
FORCEINLINE TArray()
{
}
FORCEINLINE ~TArray()
{
m_Allocator.Destroy();
}
FORCEINLINE TArray(const uint32 InElementCount)
{
m_Allocator.Resize<T>(InElementCount, m_ElementCount);
m_ElementCount = InElementCount;
}
FORCEINLINE void Add(const T &InValue)
{
}
FORCEINLINE T* GetData() const { return m_Allocator.GetAllocator<T>(); }
FORCEINLINE uint32 Num() const
{
return m_ElementCount;
}
FORCEINLINE T& operator[](uint32 InElementIndex) const
{
check(m_ElementCount > InElementIndex);
return GetData()[InElementIndex];
}
};
namespace Tests
{
FORCEINLINE void TestArrays()
{
{
TArray<float> data(10);
check(data.Num() == 10);
for(uint32 i = 0; i < data.Num(); i++)
{
data[i] = 2.0f;
}
check(data[0] == 2.0f);
}
{
TArray<uint32> data(5);
data[0] = 10;
data[1] = 5;
check(data.Num() == 5);
check(data[0] == 10);
check(data[1] == 5);
}
{
TArray<float> data(1);
data[2] = 2.0f;
}
}
}
When i try to compile(Im using Visual Studio 2017) it gives me an execption error at this position in Memory.h
static void* Copy(void *InDestination, const void *InSource, size_t InSize)
{
check_slow(InDestination);
check_slow(InSource);
check_slow(InSize > 0);
return memcpy(InDestination, InSource, InSize);
}
The test is getting called in the main function with this code:
int main()
{
Tests::TestAssertion();
Tests::TestMemory();
Tests::TestAllocator();
Tests::TestArrays();
return 0;
}
my goal currently is to look if the logger shows me the error here:
{
TArray<float> data(1);
data[2] = 2.0f;
}
here is a screenshot of the full error https://gyazo.com/6c1d6779623ffea97504f5b23f9fd7da
edit: here is the code for the allocator
#pragma once
#include <malloc.h>
#include "Core.h"
#define MEMORY_ALIGMENT 16
struct Memory
{
// TODO:Rework types.
static void* Allocate(const int32 InCount, const size_t InSize)
{
check_slow(InCount > 0);
check_slow(InSize > 0);
const size_t size = InSize * InCount;
return _aligned_malloc(size, MEMORY_ALIGMENT);
}
static void Free(void *InBlock)
{
check_slow(InBlock);
_aligned_free(InBlock);
}
static void* Copy(void *InDestination, const void *InSource, size_t InSize)
{
check_slow(InDestination);
check_slow(InSource);
check_slow(InSize > 0);
return memcpy(InDestination, InSource, InSize);
}
};
void* operator new (size_t InSize)
{
return Memory::Allocate(1, InSize);
}
void operator delete (void* InBlock)
{
Memory::Free(InBlock);
}
namespace Tests
{
struct MemoryTestStruct
{
uint32 p0;
uint32 p1;
uint32 p2;
uint32 p3;
};
FORCEINLINE void TestMemory()
{
MemoryTestStruct *t = new MemoryTestStruct();
check(t);
delete t;
}
}
edit2: Here is the code for the StackAlloctor
#pragma once
#include "../Core.h"
class DynamicLinearStackAllocator
{
private:
void *m_Data;
public:
template <typename T>
FORCEINLINE void Resize(const uint32 InElementCount, const uint32 InPreviousElementCount)
{
void *temp = Memory::Allocate(InElementCount, sizeof(T));
if (InPreviousElementCount > 0)
{
const SIZE_T size = sizeof(T) * InPreviousElementCount;
Memory::Copy(temp, m_Data, size);
Memory::Free(m_Data);
}
m_Data = temp;
}
template <typename T>
FORCEINLINE T* GetAllocator() const
{
return (T*)m_Data;
}
FORCEINLINE void Destroy()
{
Memory::Free(m_Data);
}
};
namespace Tests
{
FORCEINLINE void TestAllocator()
{
DynamicLinearStackAllocator alloc;
alloc.Resize<float>(2, 0);
alloc.Destroy();
}
}
You're not showing the code for your Allocator, but the problem is probably this line in your TArray constructor:
m_Allocator.Resize<T>(InElementCount, m_ElementCount);
At this point, m_ElementCount has not been initialized and will have some random value in it. Resize is then probably trying to free up memory that hasn't been allocated (because of the uninitialized value in m_ElementCount). You should pass in a 0 for the second parameter of the Resize call in your constructor
m_Allocator.Resize<T>(InElementCount, 0);
since there is no existing allocated memory to free.
Also, your default constructor for TArray should initialize m_Allocator.m_data to nullptr (or add a default constructor to DynamicLinearStackAllocator to do that) and set m_ElementCount to 0.
I am importing a mat file to my C++ code. After importing data, performing calculations and saving to another place I want to free the memory occupied by the original data.
Is there any specific function to perform that. Would just deleting the pointer returned by mxGetData() free the memory?
This is the class I have created to import mat file
#ifndef READMAT_H
#define READMAT_H
#include "mat.h"
#include "matrix.h"
#include "mex.h"
#include "program_exception.h"
#include "stdint.h"
class readmat
{
private:
const size_t *dimarray;
const char **dir;
MATFile *pmat;
int ndir;
mxArray *painfo,*pa;
const char *file;
int minute;
int second;
const char *name;
const char *name1;
bool isdouble;
no_mat_exception noMAT;
public:
//with default value
readmat(const char *f);
// get number of dimensions
int getnumbrofdimensions() const;
// get pointer to array
void* getarraypointer() const;
// get pointer to each dimension size
const size_t* dimensionpointer() const;
//number of elements
int numberofelements() const;
~readmat();
};
#endif
The following is the cpp implementation
#include "readmat.h"
#include <iostream>
#include "mat.h"
#include "matrix.h"
#include "mex.h"
using namespace std;
// set the file name
readmat::readmat(const char *f)
{
file = f;
pmat = matOpen(file, "r");
if (pmat == NULL) {
throw noMAT;
}
else
{
dir = (const char **)matGetDir(pmat, &ndir);
if (dir == NULL) {
printf("Error reading directory of file %s\n", file);
}
else if (ndir > 1)
{
cout << "The number of variables are larger than 1" << endl;
}
else
{
mxFree(dir);
matClose(pmat);
pmat = matOpen(file, "r");
if (pmat == NULL) {
throw noMAT;
}
else
{
painfo = matGetNextVariableInfo(pmat, &name);
matClose(pmat);
}
pmat = matOpen(file, "r");
if (pmat == NULL) {
throw noMAT;
}
else
{
pa = matGetNextVariable(pmat, &name1);
matClose(pmat);
}
}
}
}
int readmat::getnumbrofdimensions() const
{
return mxGetNumberOfDimensions(painfo);
}
void* readmat::getarraypointer() const
{
//return mxGetPr(pa);
return mxGetData(pa);
}
const size_t* readmat::dimensionpointer() const
{
return mxGetDimensions(painfo);
}
int readmat::numberofelements() const
{
return mxGetNumberOfElements(painfo);
}
readmat::~readmat()
{
mxFree(pa);
mxFree(painfo);
}
Here when I delete the object program trigger a break point in the file free.c.
The MATLAB demo on edit([matlabroot '/extern/examples/eng_mat/matdgns.c']); seems to suggest using mxDestroyArray instead of mxFree.
When calling mxGetData(). The only thing the function does, is returning a pointer to the real (in contrast to imaginary) data that is stored in the mxArray.
So there is no need to free memory, since nothing is dynamically allocated during this call.
I am trying to implement a priority queue using using a simple linear approach as explained in Art of Multiprocessor programming. I'm new to c++ and have difficulty troubleshooting.
I've implemented two template classes and am testing them using a simple test method. As I'm not able to pin point the error, I'm pasting all the three classes below for reference.
I know that _M_ construct null not valid comes when trying to construct a string using nullptr, but am not sure where I'm doing that.
The three classes created are given below:
bin.h
#include <mutex>
#include <deque>
#include <memory>
#include <iostream>
using namespace std;
namespace priority
{
template<typename T>
class Bin
{
private:
std::deque<T> v;
std::mutex m;
public:
Bin() {
}
Bin(const Bin &o) {
}
const Bin &operator=(const Bin &other) {
return *this;
}
void put(T item) {
std::lock_guard<std::mutex> lock(m);
v.push_back(item);
}
T *get() {
std::lock_guard<std::mutex> lock(m);
if (v.size() == 0) {
return nullptr;
}
else {
T val = v.front();
T *ptr_val = &(val);
v.pop_front();
return ptr_val;
}
}
bool isEmpty() {
std::lock_guard<std::mutex> lock(m);
return v.size() == 0;
}
};
}
SimpleLinear.h
#include <mutex>
#include <vector>
#include <memory>
#include "Bin.h"
namespace priority
{
template<typename T>
class SimpleLinear
{
private:
int range;
std::vector<Bin<T>> pqueue;
public:
SimpleLinear(int range){
this->range = range;
for (int i = 0; i < range; i++)
{
pqueue.push_back(Bin<T>());
}
}
void add(T item, int key) {
pqueue[key].put(item);
}
T removeMin() {
for (int i = 0; i < range; i++)
{
T *item = pqueue[i].get();
if (item != nullptr) {
return *item;
}
}
return nullptr;
}
};
}
test.cpp
#include <iostream>
#include <vector>
#include <thread>
#include <algorithm>
#include "SimpleLinear.h"
using namespace std;
using namespace priority;
void te(SimpleLinear<string> s, int thread_id) {
s.add("sundar"+to_string(thread_id), thread_id);
s.add("akshaya"+to_string(thread_id), 3);
s.add("anirudh"+to_string(thread_id), 1);
s.add("aaditya"+to_string(thread_id), 5);
cout << s.removeMin() << endl;
cout << s.removeMin() << endl;
cout << s.removeMin() << endl;
}
int main(int argc, char const *argv[])
{
SimpleLinear<string> s(100);
std::vector<std::thread> v;
for (int i = 0; i < 100; i++)
{
// if (i % 2 == 0)
v.push_back(thread(te, std::ref(s), i));
// else
// v.push_back(thread(t, std::ref(s), i));
}
for_each(v.begin(), v.end(), std::mem_fn(&std::thread::join));
return 0;
}
I'm getting the error:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
terminate called recursively
terminate called recursively
terminate called recursively
Aborted (core dumped)
One reason it crashes is that in SimpleLinear<T>::removeMin when T is std::string, return nullptr constructs a string from nullptr. basic_string::_M_construct null not valid basically says
that std::string(nullptr) was invoked.
Another reason it may crash is that get function returns a pointer to a local variable. Local variables get destroyed when the function returns. This leads to undefined behavior.
A fix:
bool get(T& result) {
std::lock_guard<std::mutex> lock(m);
if (v.empty())
return false;
result = v.front();
v.pop_front();
return true;
}
And invoke it like:
T removeMin() {
T result;
for(int i = 0; i < range; i++)
if(pqueue[i].get(result))
break;
return result;
}