how to implement the function getmsgwait() from memory queue?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi there,

I made a shared memory queue, and i implemented the function getmsg() from
the queue.

the function getmsg() works like this

getmsg()
{
lock the queue
check the stored count
read the stored item
unlock the queue
}

if there is no stored item, i have to call getmsg() function continuously.
i don't like this.
i would like to make function, getmsgwait(int waittime) which can wait until
some item is stored in the queue or waittime expires.

Would anyone like to tell me how to implement it

Thanks in advance,

Jaguk Ku


0
Reply Jaguk 9/7/2004 12:42:49 PM

See related articles to this posting


Jaguk Ku wrote:
> Hi there,
> 
> I made a shared memory queue, and i implemented the function getmsg() from
> the queue.
> 
> the function getmsg() works like this
> 
> getmsg()
> {
> lock the queue
> check the stored count
> read the stored item
> unlock the queue
> }
> 
> if there is no stored item, i have to call getmsg() function continuously.
> i don't like this.
> i would like to make function, getmsgwait(int waittime) which can wait until
> some item is stored in the queue or waittime expires.
> 
> Would anyone like to tell me how to implement it

use condition variables and broadcast insertion of a new item into the queue.
in posix world, it's pthread_cond_init(3)/pthread_cond_broadcast(3).
Use pthread_cond_timedwait(3) to implement waittime.

-- 
Lev Walkin
vlm@lionet.info
0
Reply Lev 9/7/2004 1:13:46 PM

Jaguk Ku wrote:

> Hi there,
> 
> I made a shared memory queue, and i implemented the function getmsg() from
> the queue.
> 
> the function getmsg() works like this
> 
> getmsg()
> {
> lock the queue
> check the stored count
> read the stored item
> unlock the queue
> }
> 
> if there is no stored item, i have to call getmsg() function continuously.
> i don't like this.
> i would like to make function, getmsgwait(int waittime) which can wait until
> some item is stored in the queue or waittime expires.
> 
> Would anyone like to tell me how to implement it

I did just that of I understand you correctly, you can probably use this;
http://utelsystems.dyndns.org/code/threadqueue/
0
Reply ISO 9/7/2004 1:33:07 PM

Jaguk Ku wrote:
> Hi there,
> 
> I made a shared memory queue, and i implemented the function getmsg() from
> the queue.
> 
> the function getmsg() works like this
> 
> getmsg()
> {
> lock the queue
> check the stored count
> read the stored item
> unlock the queue
> }
> 
> if there is no stored item, i have to call getmsg() function continuously.
> i don't like this.
> i would like to make function, getmsgwait(int waittime) which can wait until
> some item is stored in the queue or waittime expires.
> 
> Would anyone like to tell me how to implement it

One way would be to create a semaphore (man semop, semctl and semget 
also intro(2)).  Writers increment it, readers decrement it.  Readers 
can block on it until it is > 0.

-- ced

> 
> Thanks in advance,
> 
> Jaguk Ku
> 
> 


-- 
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
0
Reply Chuck 9/7/2004 1:51:36 PM

Nils O. Sel�sdal wrote:

>> I made a shared memory queue, and i implemented the function getmsg() 
>> from
>> the queue.
>>
>> the function getmsg() works like this
>>
>> getmsg()
>> {
>> lock the queue
>> check the stored count
>> read the stored item
>> unlock the queue
>> }
>>
>> if there is no stored item, i have to call getmsg() function 
>> continuously.
>> i don't like this.
>> i would like to make function, getmsgwait(int waittime) which can wait 
>> until
>> some item is stored in the queue or waittime expires.
>>
>> Would anyone like to tell me how to implement it
> 
> 
> I did just that of I understand you correctly, you can probably use this;
> http://utelsystems.dyndns.org/code/threadqueue/

On second thought, i perhaps did not understand you. The above is for
threads only. Not between processes. If you need that, semaphores
might be what you need. man semop,semctl,semget.
0
Reply ISO 9/7/2004 1:57:47 PM
comp.unix.programmer 10672 articles. 64 followers. Post

4 Replies
211 Views

Similar Articles

[PageSpeed] 44


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

implement a queue as shared memory
I am trying to figure out how to implement a queue using shared memory. I want to do this so 2 threads can access this queue, but with mutual exclusion. say I wanted a queue of max size 5 integers int seg_id = shmget(IPC_PRIVATE, 20480, S_IRUSR | S_WRUSR); //this gets my shared memory but how do I make this shared memory a queue? could I do this: queue<int> *shared_queue; shared_queue = (int *) shmat(seg_id, NULL, 0); would this work? would this allow me to still use the common queue functions like pop, push, front? am I way off on this? Leshanster <leshanster@gmail.com>...

how to implement a queue-like container with sort function
I want to a fixed length list-like container, it should have a sorted()-lik= e function that I can use to sort it,I think there should also a function I= can use it to detect whether the numbers of items in it reaches the length= of the container , because if the numbers of items in it reaches the lengt= h(fixed) of the container,I want to process the data in it .Is there a cont= ainer in Python like this ?If not, what base container should be used to im= plement such container? the container is similar to queue ,but queue doesn't have a sort function= =20 On Fri, Nov 29, 20...

Internal of C/C++ run time library (Module offers a function that allocates memory, must also offer a function that frees memory)??
Single address space consists of one executable module and several DLL modules. Some of these modules can link to a static version of the C/C + + run-time library, some of these modules might link to a DLL version of the C/C++ run-time library, and some of these modules (if not written in C/C++) might not require the C/C++ run-time library at all. Please put some light on how C/C++ runtime library malloc or free functions works in a address space and how static linking or dynamic linking can affect the allocation or deallocation of a memory in a single address space. Thanks in a...

call a function within a (function() {...})(); implementation
How do you call a function within an implementation that's in this form: (function () { var G_vmlCanvasManager_ = { init: function() { ... }, initElement: function (el) { ... } ... })(); I'm trying to call the initElement function that's within excanvas.js which its comment says is public. If I try calling initElement(c) or G_vmlCanvasManager_.initElement(c) IE gives errors. Andrew Poulos Ok, I missed the externs at the bottom of the library and the version of excanvas I was using did not return the element when you initialised a new element....

Out of memory & Function MEMORY is not available on this platform
well guys is very strange... since: when I type: help memory I am to get response from Matlab as: .... Example 1: Run the MEMORY command on a 32-bit Windows system: .... Example 2: Run the MEMORY command on a 64-bit Windows system: .... Example 3: Run the MEMORY command with two outputs on a 32-bit Windows system: [uV sV] = memory .... but when I type: memory ??? Error using ==> memory Function MEMORY is not available on this platform. Its kind of strange isn't it? btw I use MAC OS X 10.6 (might this be a problem?) Moreover I would like to execute this ...

implementation of rotation function without built-in function
i am given a task for rotating the image without using imrotate function, i am using affine function for the task. this is what i have done deg=75; orignal_img = imread('path of the image'); [row col]= size(orignal_img); affinematrix=[cos(deg) sin(deg) 0; -sin(deg) cos(deg) 0; 0 0 1]; for x=1:row for y=1:col rot(x*y)=orignal_img(x*y)*affinematrix; end end as i am doing pixel by pixel rotation, i don't get the logic of the error ??? Error using ==> times Class of operand is not supported. can anyone please guide me that what is missing...

How memory function free() knows how much memory to free.
Hi, How does the memory management function 'free()' frees the memory pointed by a pointer variable at run time. how does it know the size. if there is way when free() can do, then why can't we do it externally?. Thanks Panduranga Chary > Hi, > > How does the memory management function 'free()' frees the memory > pointed by a pointer variable at run time. how does it know the size. > if there is way when free() can do, then why can't we do it > externally?. When you malloc memory, the size of the allocated block is stored somewhere. What do you...

Implementing an STL memory pool with constant memory locations
I'm asking for a lot here but what I would like to do is create a dynamically growing memory pool whose addresses don't change. I understand I can't do this with std::vector so for the explanation I'll use an imaginary class called pool. What I mean: pool.push_back(object); // Increases memory size by a block size, say 10 elements After 10 push_back's pool.push_back(object); // APPEND 10 additional memory block sizes BUT elements 1 thru 10 retain their memory locations. What I'm trying to do: I am trying to use a tree whose elements maintain a key and a pointer t...

virtual function and pure virtual function in the implementation of COM
Hello All, Please excuse me for posting this here, but I don't find any other group where I will get a proper answer. This is about clarifying the C++ part of COM. I understand that COM is a mechanism by which interface and implementation is seperated. A basic com implementation in c++ can be as follows. // Dev-CPP // interface // Ai.h class Ai { public: virtual void foo() = 0; virtual Ai* GetInterface() =0; }; // Implementation // A.h #include "Ai.h" class AImpl : public Ai { public: void foo(); Ai* GetInterface(); }; // A.cpp #include "A.h&qu...

function implementation
Hi all, C comes with standard libraries. Let say I am interested in one function called strlen from string.h. And here is the declaration (http://www.acm.uiuc.edu/webmonkeys/book/c_guide/): Declaration: size_t strlen(const char *str); Computes the length of the string str up to but not including the terminating null character. Returns the number of characters in the string. My question is: is it possible for me to read its definition or to know how this function is implemented? Thanks, Li chen_li3@yahoo.com said: > Hi all, > > C comes with standard libraries. Let say I ...

function implementation
Hi I am trying to code the following function in matlab, V = ( B/(k-1) ) / (W / (N-k)) ; B is sum squares between U # of vectors (unknown) W is sum of squares between L # of vectors (unknown) k - number of clusters (known) N - number of vectors (known) "Al B" <bourisly@gmail.com> wrote in message <gkqoh1$rat$1@fred.mathworks.com>... > Hi I am trying to code the following function in matlab, > > V = ( B/(k-1) ) / (W / (N-k)) ; > > B is sum squares between U # of vectors (unknown) > W is sum of squares between L # of vectors (unknown) > > k -...

implementation of $() functionality
Hello I want to implement the following functionality. (It might be a bit similar to the implementation of $() functionality of scripting languages.) Suppose I have a string - generated or inputted. I want my program to invoke a function of the same name. Obviously, I can do this with an if-else ladder. However, that would be really poor implementation when the number of the strings is large. (It might be something like dynamically invoking the compiler.) Thank you Cross On Jun 12, 8:16=A0am, Cross <X...@X.tv> wrote: > Suppose I have a string - generated or inputted. I want my pro...

Implementing function handles when compiling GUI-based callback functions
I am having issues trying to compile a program with graphics created in the Matlab GUI development environment. I created the program and then tried to compile it using the Visual Studio Matlab Add-in. The first graphic screen comes up, but when I press on a button or perform any action that calls a new m-file, I get errors in the run-time executable screen. I called Matlab and they told me I had to implement function handles to call/execute a new m-file in the program, but there is limited documentation for this. I can't seem to figure out how to implement this properly. Has...

how to implement this function?
Hi: I have a rectangular matrix A of size N*N. Its top-left corner submatrix is B of size of n*n, where N>n. I'd like to redistribute the elements not inside B randomly, just like the following example: 1 1 2 2 3 4 1 1 3 4 2 1 2 3 2 4 2 3 2 1 3 2 3 4 where the top-left corner submatrix [1 1; 1 1] should be kept at the original places and the other elements are to be redistributed randomly. How can I implement it in Matlab efficiently? Thanks Gui Xie wrote: > > > Hi: > I have a rectangular matrix A of size N*N. Its top-left corner > submatrix > is B of si...

How to implement this function so that the vector can be passed back to the calling function?
I wrote a function which looks like: bool Xxx::findShortestPath(int xS, int yS, int xT, int yT, vector<Vertex*> path) { .... } It meant to find a path, save the path in vector<Vertex*> path, and pass the path back to the calling function. And in some other function (say, main()), I called this function like: int main() { .... vector<Vertex*> path; xxx.findShortestPath(0, 0, 9, 9, path); // then print out the path } However, I just noticed that it failed to passed the path to the main() function. That is, in the function findShortestPath(...), the data has been loaded in...

Implementation of a function
Hi, I would like to ask a question about implementation of a function in Matlab. Let me explain my question: In my problem, it is necessary to define a fuction in the form: f(t) = a*exp(bt) in which a and b can be analytically expressed as a = a_1*s + a_2*n and b = b_1*s +b_2*n where s, n are arrays of known integer values. (For example, a = 1:3 and b = 1:5.) We have f(a, b, t) = (a_1*s + a_2*n)*exp[(b_1*s +b_2*n)*t]. I want to fit the function to some observed data so I have to minimize the sum of square. We'll take a mechanism as follow. Let's consider the trivial case, where a...

Memory() function
Hi VO developers, has somebody a function that receives correct values of total and available physikal memory for systems with memory greater then 2 GB? VO function memory() returns uncorrect values. I think the API function GlobalMemoryStatusEx() must be used therefore. But GlobalMemoryStatusex() is only available on systems using the OS WIN2000 or greater. I need a function that also works on WIN NT and WIN95. Regards, Fred Killet killet-nospam@killetsoft.de Got it by my self. I tested it and It runs under WINXP. Wold you have a closer look to the function MemoryE...

Does anyone know why C++ std queue is faster than my C queue implementation?
I have written several C queue implementations that range from a separate compilation unit to a complete macro version and all of them are significantly slower than their C++ counterpart. I would really like to know what I am doing wrong. At very small queue sizes/loop iterations or when compilation optimization is turned off, my C implementations are faster but in all other cases they are not. When I run strace, I can see that the biggest difference between C and C++ is that realloc is using mremap and new is mostly using brk to dynamically allocate memory. mremap is obviously slower but ...

New implementation of FSM using functions returning pointers to functions of the same type!
Hi Everyone, Before I go into details I would like to thank Svillen Ranev my professor for solving the "How to define, declare and call a function returning function pointers of the same type?" question I had asked in the context of my compilers class. The problem is this: a function foo foo() taking as parameter a char foo(char) returning a pointer *foo(char) to a function (of the same type) (*foo(char))() taking char (*foo(char))(char) returning a pointer *(*foo(char))(char) to a function (*(*foo(char))(char))() taking char (*(*foo(char))(char))(char) and so on..... This goes on ...

Inheritance: Implementing pure virtual functions by inheriting from an object providing an implementation
Hi I'm wondering if an idea that I have should work. Well the compiler (mingw) does not seem to allow it, but I'm not sure why it does not work. I have an interface class: class IManagable { public: virtual void setID(int new_id) = 0; virtual int getID() = 0; } And an object implementing this interface: class ManagableObject { public: ManagableObject () { id = -1; } ~ManagableObject (); // Implement functions inline void setID(int new_id) { id = new_id; } inline int getID() { return id; } private: int id; } I've read in the FAQ that inheritanc...

Is std::tr1::function intended to work ONLY with function objects which implement the operator() as const?
Hi, I'm using an STL implementation that fails to compile the below code. So the question is: is the below code standard? Or the behavior is implementation-specific? ------------------------------------------------------------------------------ #include <functional> struct functor { void operator() (int) { } // not const }; int main() { functor fun; std::tr1::function<void(int)> f = fun; // Is this standard? return 0; } ------------------------------------------------------------------------------ My understanding is that the c...

Single producer, "one-shot" work queue implementation, no lock on queue.
I have a bug in a work queue implementation I'm trying, and I can't think of a good solution. I have a list of work items that need to be processed periodically, and the list can be constructed completely before processing begins. There is a single thread that builds the list, and multiple worker threads that perform the work concurrently while the single thread waits for them all to finish. It's not a continuous work queue; the producer thread adds items, says "go!", the worker threads do it, and then the producer thread obtains the results and continues on ...

implementing a private work queue in each process with job-stealing and a pthread listener to listen job-stealing requests from other queues
I am trying to implemenrt a task queue system where each process has its own task queue. In main(), each process empties its own task queue first, then it tries to ask other processes for jobs. After MPI is initialized and just before emptying its own task queue, a listener pthread is started to listen job requests from others. The job-stealing requests are in main() but the request-receiving call and job sending call are in the pthread. I tried to program but it hangs as soons as the program starts asking for jobs. Then I suspect there mayt be some problems and I wrote a simpler toy exam...

How to implement function like this?
Hello gurus, I have a question, a function like below, it is implemented by me, :) def funcA(tarray): a = [2,3,4] if len(tarray) >=3: return a[0],a[1], a[2] elif len(tarray) == 2: return a[0],a[1], funcB(1)[0] elif len(tarray) == 1: return a[0], funcB(2)[0], funcB(2)[1] else: return funcB(3)[0], funcB(3)[1], funcB(3)[2] The return of funcA is always 3 values, but depending on the length of tarray, I need to return different values accordingly. if tarray lenght is 2, I need to get another one value ...