473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Porable Semaphore implemented in C++ ?

Hi,

i am writting some classes, those need to work in both linux and
windows. in my problem, there are mutiple productor and one
consumer, productors each in a different thread are trying to append a
byte to a shared buffer while the consumer in the main thread will
remove one byte every some milliseconds.

the productors has to wait if the buffer is read/write by other
productors or the consumer, on the other hand, the consumer should
immediately exit if the buffer is read/write by any productor.

i am thinking, for the problem above, the semaphore can help. is
there a portable semaphore implementation in C++ ( if not, in C )?

thanks in advance.

-
woody

Oct 1 '07 #1
5 3124
Steven Woody wrote:
Hi,

i am writting some classes, those need to work in both linux and
windows. in my problem, there are mutiple productor and one
consumer, productors each in a different thread are trying to append a
byte to a shared buffer while the consumer in the main thread will
remove one byte every some milliseconds.

the productors has to wait if the buffer is read/write by other
productors or the consumer, on the other hand, the consumer should
immediately exit if the buffer is read/write by any productor.

i am thinking, for the problem above, the semaphore can help. is
there a portable semaphore implementation in C++ ( if not, in C )?

thanks in advance.
A good idea in my opinion would be to look for the boost thread library.
It's not standard, but it's portable enough. It hasn't got semaphores,
though. However, I think a mutex solution can be devised easily and it
may be less error-prone.

Regards,

Zeppe
Oct 1 '07 #2
On Oct 1, 5:08 pm, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
Steven Woody wrote:
Hi,
i am writting some classes, those need to work in both linux and
windows. in my problem, there are mutiple productor and one
consumer, productors each in a different thread are trying to append a
byte to a shared buffer while the consumer in the main thread will
remove one byte every some milliseconds.
the productors has to wait if the buffer is read/write by other
productors or the consumer, on the other hand, the consumer should
immediately exit if the buffer is read/write by any productor.
i am thinking, for the problem above, the semaphore can help. is
there a portable semaphore implementation in C++ ( if not, in C )?
thanks in advance.

A good idea in my opinion would be to look for the boost thread library.
It's not standard, but it's portable enough. It hasn't got semaphores,
though. However, I think a mutex solution can be devised easily and it
may be less error-prone.

Regards,

Zeppe
ok, i go to study it. but i have one thing in doubt: semaphores or
other mutex solutions should have to dependent on some sort of 'atomic
operation' which should be OS-dependent, am i right? so how can a
library implement it without depends on OS?

ps. how to implement atomic oepration in Linux and how to implement it
in Windows? thanks.
Oct 1 '07 #3
Steven Woody wrote:
On Oct 1, 5:08 pm, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
>Steven Woody wrote:
>>Hi,
i am writting some classes, those need to work in both linux and
windows. in my problem, there are mutiple productor and one
consumer, productors each in a different thread are trying to append a
byte to a shared buffer while the consumer in the main thread will
remove one byte every some milliseconds.
the productors has to wait if the buffer is read/write by other
productors or the consumer, on the other hand, the consumer should
immediately exit if the buffer is read/write by any productor.
i am thinking, for the problem above, the semaphore can help. is
there a portable semaphore implementation in C++ ( if not, in C )?
thanks in advance.
A good idea in my opinion would be to look for the boost thread library.
It's not standard, but it's portable enough. It hasn't got semaphores,
though. However, I think a mutex solution can be devised easily and it
may be less error-prone.

Regards,

Zeppe

ok, i go to study it. but i have one thing in doubt: semaphores or
other mutex solutions should have to dependent on some sort of 'atomic
operation' which should be OS-dependent, am i right? so how can a
library implement it without depends on OS?
Yes, it is OS-dependent, and a library can implement it with
#ifdef something
// implementation for window
#else
// implementation for linux
#endif

>
ps. how to implement atomic oepration in Linux and how to implement it
in Windows? thanks.

You are better asking those questions in another news group. Maybe
comp.programming.threads

Oct 1 '07 #4
An unknown Norwegian <an**@no.nowrote:
Yes, it is OS-dependent, and a library can implement it with
#ifdef something
// implementation for window
#else
// implementation for linux
#endif
The Juce framework http://www.rawmaterialsoftware.com/juce/ has
some thread stuff that might prove helpful...
Oct 1 '07 #5
Steven Woody <na********@gmail.comwrites:

[...]
thanks, i decide to study Boost.thread. i just don't know if it is
hard to learn. i don't get many days left :(
I found it very straightforward. This article was helpful in getting
started:

http://www.ddj.com/cpp/184401518

---Scott.
Oct 2 '07 #6

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

Similar topics

7
by: Bastian Hammer | last post by:
Hi I´m wondering why there are so few examples with Semaphore. Is it obsolete? I´ve got a Class Data. It offers 2 Threads methods for updating, editing, .. a private dictionary. Now I...
1
by: rushik | last post by:
Hello all, I m facing an interesting problem related to semaphore in php. We are using a huge business application running on LAMP. For database operations we are maintaining centralized DB...
5
by: marvind | last post by:
I tried using a Semaphore class (have included the full listing reproduced from article Figure 1 at the end of this email) in .NET 1.1. It works fine most of the time, however, I see the following...
5
by: Unni | last post by:
Hello all, I m facing a memory related problem with semaphores. Our business application uses semaphores extensively and the limit imposed by the OS on the number of semaphores that can exist on...
2
by: techi_C | last post by:
Hi I'm getting a problem while removing semaphore from system. Before removing semaphore I'm checking the usage count of a smaphore. // checking usage count usage_count =...
1
by: cranfic | last post by:
Please help... To make it simple, I have a simple test C program which calls my semphore library functions only. (It doesn't call any db2 function at all). And the following is the test...
0
by: sukasa | last post by:
Greetings. After first noticing a malfunction in my application, I ran some tests which have given me the following insights on the code performance: -The " infinite" loop which waits on...
0
by: Samuel R. Neff | last post by:
I'm having trouble creating a Semaphore with read-access rights for everyone. Originally I was trying to use this code: semaphore = new Semaphore(maxLocks, maxLocks, "RwLock#" + name); but...
5
by: GHUM | last post by:
hello, in my application I am using hSem = win32event.CreateSemaphore (None, 1, 1,"stringincludinginterfaceandport") rt=win32event.WaitForSingleObject (hSem, 0) if rt !=...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.