f



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

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
Jaguk
9/7/2004 12:42:49 PM
comp.unix.programmer 10826 articles. 0 followers. kokososo56 (349) is leader. Post Follow

4 Replies
346 Views

Similar Articles

[PageSpeed] 21

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
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
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
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
ISO
9/7/2004 1:57:47 PM
Reply: