473,800 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copying mutexes, cv and pthread_ts

Hi All,

I have a class that creates a thread, a mutex and a condition variable
in its constructor. I am writing a copy constructor for this class in
C++. I am doing a simple copy using the member initialization list.
First of all, does anyone have any opinion about whether this will work
fine. I think it will. I believe a copy constructor, when using a
member initialization list, does a memory copy of the object's members
to be copied. In which case I believe this should work.
Does anyone have a different opinion about the feasibility of this? The
reason I am trying to do this copy constructor is to push this class
into a vector container. But I am not sure about what will happen when
I try to erase this element from the vector. I am gonna do the
destructor later, I have the destructor figured out though.

Ciao, Draw

May 21 '06 #1
7 5716
dr****@rediffma il.com writes:
I have a class that creates a thread, a mutex and a condition
variable in its constructor. I am writing a copy constructor
for this class in C++. I am doing a simple copy using the member
initialization list. First of all, does anyone have any opinion
about whether this will work fine.


It will not. Mutexes and conditions are non-copyable.

They may refer to dynamically allocated memory, or they may include
addresses registered with the OS. They are typically implemented in C,
so there is no C++ copy constructor nor assignment operator defined
for them, and thus you will typically get a bitwise copy rather than
an error, which doesn't need to yield a sensible semantics however.

Besides, it's not clear what would it mean to copy them if somebody is
waitng on them, or whether a copy of a locked mutex should be locked.
They are non-copyable conceptually, not only technically.

It's possible that for a class which includes a mutex, a sensible copy
constructor should freshly initialize the mutex in the copy. Possibly
while keeping the mutex of the original locked, to prevent taking the
snapshot of data which is being modified by another thread. This all
depends on the locking policy.

Disclaimer: I've never tried to make a copyable object which includes
a mutex, this is all guessing from my head.

--
__("< Marcin Kowalczyk
\__/ qr****@knm.org. pl
^^ http://qrnik.knm.org.pl/~qrczak/
May 21 '06 #2
dr****@rediffma il.com wrote:
Hi All,

I have a class that creates a thread, a mutex and a condition variable
in its constructor. I am writing a copy constructor for this class in
C++. I am doing a simple copy using the member initialization list.
First of all, does anyone have any opinion about whether this will work
fine. I think it will. I believe a copy constructor, when using a
member initialization list, does a memory copy of the object's members
to be copied. In which case I believe this should work.
Does anyone have a different opinion about the feasibility of this? The
reason I am trying to do this copy constructor is to push this class
into a vector container. But I am not sure about what will happen when
I try to erase this element from the vector. I am gonna do the
destructor later, I have the destructor figured out though.

Apart from not being possible, does it make any sense to copy them?

If you require locking on each instance of the class, you have to
initialise a new mutex and cv for each instance. If you require global
locking for all instances of the class, use static members.

--
Ian Collins.
May 21 '06 #3

Ian Collins wrote:
dr****@rediffma il.com wrote:
Hi All,
Apart from not being possible, does it make any sense to copy them?

If you require locking on each instance of the class, you have to
initialise a new mutex and cv for each instance. If you require global
locking for all instances of the class, use static members.


No you don't want static members. Just because you can't copy the class
doesn't mean you can't have more than one instance.

If you want to share this collection aruond then use shared_ptr. (It's
in tr1 and boost). The only worry is that shared_ptr isn't strictly
thread-safe but actually the only issue is in deletion if two threads
decrease the reference count at the same time and then either both or
neither thread invokes the delete. With proper marshalling you can
avoid this problem.

Jun 9 '06 #4
Earl Purple wrote:
Ian Collins wrote:
dr****@rediff mail.com wrote:
Hi All,

Apart from not being possible, does it make any sense to copy them?

If you require locking on each instance of the class, you have to
initialise a new mutex and cv for each instance. If you require global
locking for all instances of the class, use static members.

No you don't want static members.


Why not?
Just because you can't copy the class doesn't mean you can't have more than one instance.
Isn't that what I said?
If you want to share this collection aruond then use shared_ptr. (It's
in tr1 and boost). The only worry is that shared_ptr isn't strictly
thread-safe but actually the only issue is in deletion if two threads
decrease the reference count at the same time and then either both or
neither thread invokes the delete. With proper marshalling you can
avoid this problem.

What benefit does this offer over a static member?

--
Ian Collins.
Jun 9 '06 #5

Ian Collins wrote:
Earl Purple wrote:

No you don't want static members.
Why not?


Because a static member is the same for every instance of the class.
Just because you can't copy the class doesn't mean you can't have more than one instance.

Isn't that what I said?


But having a static member means a variable that is shared between
every instance of your class. Do you really want that?
If you want to share this collection aruond then use shared_ptr. (It's
in tr1 and boost). The only worry is that shared_ptr isn't strictly
thread-safe but actually the only issue is in deletion if two threads
decrease the reference count at the same time and then either both or
neither thread invokes the delete. With proper marshalling you can
avoid this problem.

What benefit does this offer over a static member?


shared_ptr simply gives reference counting to pointers. Pointers are
weak-references to the same class. When you copy a shared_ptr you
simply up its reference count so you do not need to handle the memory
management, and when the last reference is destroyed, the class is
deleted, with its destructor called.

The big advantage of C++ is RAII which is basically the automatic use
of destructors.

Ian Collins.


Jun 12 '06 #6
Earl Purple wrote:
Ian Collins wrote:
Earl Purple wrote:
No you don't want static members.
Why not?

Because a static member is the same for every instance of the class.

Just because you can't copy the class doesn't mean you can't have more than one instance.


Isn't that what I said?

But having a static member means a variable that is shared between
every instance of your class. Do you really want that?

If you read my original post, I said "If you require global locking for
all instances of the class, use static members." That is, one mutex
shared by all instances of the class.
If you want to share this collection aruond then use shared_ptr. (It's
in tr1 and boost). The only worry is that shared_ptr isn't strictly
thread-safe but actually the only issue is in deletion if two threads
decrease the reference count at the same time and then either both or
neither thread invokes the delete. With proper marshalling you can
avoid this problem.


What benefit does this offer over a static member?

shared_ptr simply gives reference counting to pointers. Pointers are
weak-references to the same class. When you copy a shared_ptr you
simply up its reference count so you do not need to handle the memory
management, and when the last reference is destroyed, the class is
deleted, with its destructor called.

So you advocate the used of a non-trivial class member to share a mutex
between all instances of a class over a simple static member?

--
Ian Collins.
Jun 13 '06 #7

Ian Collins wrote:
If you read my original post, I said "If you require global locking for
all instances of the class, use static members." That is, one mutex
shared by all instances of the class.
Yes, agreed, you could do it that way, but you would have to be certain
that your design is right and that you really do want to lock every
instance of a class and that such will remain the case forever. Because
once you decide you want other instances of the class not in this
"group" then you have to go back and change a lot of code.

OP was originally going to create a class then attempt to copy it using
copy-constructors etc, which suggested he did not necessarily want just
one instance, but wanted to pass a particular instance around and have
global locking on those particular instances.
So you advocate the used of a non-trivial class member to share a mutex
between all instances of a class over a simple static member?


Whilst shared_ptr is non-trivial to write, you don't have to write it,
boost have already done so. It is relatively trivial to use. The only
issue in a multi-threaded environment is a bit of marshalling to ensure
two threads don't try to delete the last reference at exactly the same
time. That's usually fairly simple to administer with a thread join.

Jun 13 '06 #8

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

Similar topics

5
9636
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing all the contents of f2 will be created and thrown away. In the case of two StringIO objects, this means there's a point when the contents is held in memory three times.
3
3657
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
4
2981
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html
22
6969
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable this option? I assume there isn't but I have to try.
0
1557
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and checks the .NET CF version. The first message I receive is: Files in the package 'netcf.all.wce4.armv4.cab' are more recent on the device than on the development computer. Consider upgrading to the latest version. Files affected are:
2
1626
by: UJ | last post by:
Is there a way to get a list of all the mutexes that have already been defined? TIA - Jeff.
1
2796
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. Here's my flow of commands: 1. copy a single row of a 2D array 2. change that single row (either add a cell or delete a cell). 3. copy that single row back into the 2D array by the use of a temporary array. So I'm basically copying a single row...
3
2223
by: pbd22 | last post by:
Hi. I have a C# program that fires an external VB6 program which writes to a file and terminates. It is ugly, but this is how I have to do it. I cannot change this part of the program. The problem I am encountering is that the former process created by this EXE is not complete (it runs for a few seconds) when the next one wants to start. A
8
2198
by: Raxit | last post by:
Hi, In Mulithreaded program, using Posix api, we do pthread_mutex_lock(&Lock)
0
9555
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10287
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
10260
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,...
0
9099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7588
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
6826
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
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...
2
3770
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2956
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.