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 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/ 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.
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.
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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
|
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
|
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.
|
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:
| |
by: UJ |
last post by:
Is there a way to get a list of all the mutexes that have already been
defined?
TIA - Jeff.
|
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...
|
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
|
by: Raxit |
last post by:
Hi,
In Mulithreaded program, using Posix api, we do
pthread_mutex_lock(&Lock)
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |