473,626 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

one producer thread, one consumer thread: mutex needed?

Hello,

I have an application that essentially consists of two threads doing
their things. One thread is a producer, and pushes bytes (of a struct)
into a pipe, and another is a consumer that simply checks the pipe at
regular intervals reads out the bytes. If the consumer extracts bytes
and sees that it's not enough to recast to the struct, it caches it and
waits for more bytes in the pipe.

My question is: Do I still need to maintain a mutex that is locked
whenever a thread writes or reads? As I mentioned already, I have only
one reader and one writer, and the reader checks if it has enough bytes
to cast into a struct.

-Thanks.

Nov 15 '05 #1
3 3516
In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
<sm*******@exci te.com> wrote:
I have an application that essentially consists of two threads doing
their things. One thread is a producer, and pushes bytes (of a struct)
into a pipe, and another is a consumer that simply checks the pipe at
regular intervals reads out the bytes. If the consumer extracts bytes
and sees that it's not enough to recast to the struct, it caches it and
waits for more bytes in the pipe. My question is: Do I still need to maintain a mutex that is locked
whenever a thread writes or reads?


One of the newsgroups you posted to is comp.lang.c, which talks
about what is possible in standard C -- talks about that which
is portable and well-defined behaviour.

In standard C, neither "pipe" nor "mutex" have any meaning.

The technical meaning of "mutex" is relatively narrow... but
it happens that there is no portable way to implement a mutex
in portable C.

There are a number of different common meanings for "pipe",
some of which are implementable in portable C and most of which are
not. In the ones which -are- implementable in portable C...
well, in a way it doesn't matter much anyhow, as portable C does not
offer any form of multiprocessing , and if you wanted to deliberately
switch your single-threaded program between producer and consumer
mode while you were in the middle of updating your "pipe" structures,
then the C language isn't going to protect you from any inconsistancies
that result.
The fact that you cross-posted to comp.unix.progr ammer likely
indicates that you had a fairly specific meaning of "pipe" in mind,
and indicates that you likely had at least minimal multiprocessing
open to you as a possibility ("fork"). But it doesn't tell us which
Unix or Unix Standard you are working against, nor which POSIX
standards that Unix complies with, and it doesn't nail down
whether you are just using fork() or if you are using popen()
or if you are using sockets, or pthreads... and it is possible you were
using "pipe" loosely... And we don't know whether the particular -kind-
of mutex you are using is sufficient to between your producer and
your consumer...
But aside from all of that, if you are using anything approximating
a recent Unix, then the answer to your question is *likely* "Yes".

When you write to a Unix pipe, the Unix I/O layer ensures that there
are no inconsistancies in the I/O presentation. For example, it will
ensure that the size of the available data is not incremented
before the data is readable, so you do not have to worry about
being told that data is there and then getting nonsense in the
buffer because it hasn't been delivered yet.

On the other hand, if your "pipe" is not a pipe() or popen() and
is instead a read/write fd or FILE* being shared between a parent
process and another process fork()'d from it, then -effectively-
you *do* need a mutex, as POSISX does not allow you to shift
a shared fd or FILE* between reading and writing without undertaking
certain precautions first. The exact set of precautions takes
at least 1 1/2 printed pages of the POSIX.1-1990 standard, and is
perhaps most easily dealt with via a mutex or at least through
advisory shared volatile variables.
--
"Never install telephone wiring during a lightning storm." -- Linksys
Nov 15 '05 #2
sm*******@excit e.com writes:
I have an application that essentially consists of two threads doing
their things. One thread is a producer, and pushes bytes (of a struct)
into a pipe, and another is a consumer that simply checks the pipe at
regular intervals reads out the bytes. If the consumer extracts bytes
and sees that it's not enough to recast to the struct, it caches it and
waits for more bytes in the pipe.

[...]

Standard C doesn't have threads. Followups redirected to
comp.unix.progr ammer. You might also try comp.programmin g.threads.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3
What you need is a shared, counted queue of structures.

In C++, one might define the queue as:

Shared<Counted< Queue<YourStruc t> > > queue;

Shared<> is wrapper template that gives any struct a mutex with
acquire()/release() members.
Counted<> is wrapper template that gives any struct a semaphore with
raise()/lower() members.

// Producer does this:
queue.acquire() ; // acquire exclusivity on queue
// Now you can put one of YourStruct into the queue
queue.release() ; // release exclusivity on queue
queue.raise(); // declare that there is now at least one new YourStruct
waiting in queue

// Consumer does:
queue.lower(); // block until at least one new YourStruct waiting in
queue
queue.acquire() ; // acquire exclusivity on the queue
// Now you can get one of YourStruct out of the queue
queue.release() ; // release exclusivity on queue

You should be able to translate this code to C without much effort.

-Chaud Lapin-

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7444
by: Debaser | last post by:
I'm getting an IllegalMonitorStateException in trying to implement the above subject. The static declararions made it work, so if this is part of the problem let me know. I think it has something to do with the synchronization. I think I'm accessing the bin when I'm not supposed to be. **This IS a homework assignment, FYI, and we are using the 1.4.2 API** So it does have to sleep for a random time, it does have to use as many consumers...
7
2358
by: Evan Simpson | last post by:
WEBoggle needs a new game board every three minutes. Boards take an unpredictable (much less than 3min, but non-trivial) amount of time to generate. The system is driven by web requests, and I don't want the request that happens to trigger the need for the new board to have to pay the time cost of generating it. I set up a producer thread that does nothing but generate boards and put them into a length-two Queue (blocking). At the rate...
2
3962
by: ian douglas | last post by:
I have one process that will be multi-threaded. The parent (A) will sit and deal with TCP/IP issues, and feed data to its child process (B) via shared memory. I need assistance in finding a good example of (B) pausing (not in a busy-wait loop) until it gets a signal from (A) that data is ready. That the child process (B) will then have a shared memory segment with a second running process (C), where (B) will be the producer for (C), so...
3
4944
by: MrNobody | last post by:
I am developing an application that has several multi threaded tasks where one thread is doing IO and another thread is grabbing data from the first thread to process it further. I've been noticing that every once in a while my program simply hangs- it is rare but it is annoying. I am worried it may be a synchronization issue involving my producer/consumer threads. I basically go like this:
2
4208
by: Ramta | last post by:
Hi all, I am trying to develop a Producer thread that listens for UDP packets on a socket and store then in an ArrayList. The consumer should read data from list and block when no element is in list. When producer inserts a message in list, it should make the consumer aware of the item and consumer should process the item as soon as it is available.
2
5471
by: Rene Ruppert | last post by:
Hi, I'm trying to implement the Producer-Consumer-Problem in C#. Below is my code. The problem is, that the buffer always contains only one element...it seems that the Thread.Sleep() in the producer and in the consumers make the whole app sleep instead of only making the current thread sleep. Maybe someone can help?
22
4083
by: Morpheus | last post by:
Hi, I have been coding in Windows for many years so have a mindset to it, so forgive any stupid questions. Is it possible to create a multithread application in C++ that is portable (OS/compiler)? In my Windows applications, I use synchronization objects like, mutex, semaphores etc. Is there anything similar in the Standard ANSI C++ or would I be best off doing this myself, possibly creating a new class to handle any critical...
4
11173
by: mps | last post by:
It seems to me that the MSDN code for synchronizing a producer and consumer thread at http://msdn2.microsoft.com/en-us/library/yy12yx1f.aspx is completely wrong. There is no reason to assume that each call to NewItemEvent.Set() will release a consumer thread (as explicitly mentioned in the AutoResetEven documentation), which means that the consumer threads won't necessarily know that there are items on the queue. Is this correct? Mike
10
4410
by: George Sakkis | last post by:
I'd like some feedback on a solution to a variant of the producer- consumer problem. My first few attempts turned out to deadlock occasionally; this one seems to be deadlock-free so far but I can't tell if it's provably correct, and if so, whether it can be simplified. The generic producer-consumer situation with unlimited buffer capacity is illustrated at http://docs.python.org/lib/condition-objects.html. That approach assumes that the...
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.