473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread safe array class

I'm implementing some different c++ classes which I want to be thread
safe. All these classes contain lists or arrays of some kind.
I'm using protected sections to make them thread safe.

The question then is: how do you in a nice safe way pick values out of
the list? The easiest way is to have a pair of Lock, Unlock functions,
but this also presents a lot of ways of doing a misstake.

Say the list class has 5 functions, one to get the number of items,
one to get items one by one and 3 other functions. The 3 functions
have their locks internally as per c++ design you don't want other
classes need to know anything about internal structure of another
class. The 2 other functions is the problem. I would have to have
external locks to lock them as the number of items might change while
going through the list otherwise. 3 problems arise with that solution:
you might forget (or don't know that it's needed) to lock the list
before using it, you might forget to unlock the list while done and
you might deadlock against other list functions that you didn't know
were blocking too (or because you thought external locks were needed
there as well).

How to solve this in a nice way? I've been thinking of perhaps
supplying a list to a special function in the list class that then
transfers all the list data in the class to the supplied list and thus
having a copy that won't risk changing. This will take double the
memory and will require an extra loop through the data though.
Sep 12 '08 #1
3 3070
On Sep 12, 12:52 am, andreas.zetters t...@gmail.com wrote:
Say the list class has 5 functions, one to get the number of items,
one to get items one by one and 3 other functions. The 3 functions
have their locks internally as per c++ design you don't want other
classes need to know anything about internal structure of another
class. The 2 other functions is the problem. I would have to have
external locks to lock them as the number of items might change while
going through the list otherwise. 3 problems arise with that solution:
you might forget (or don't know that it's needed) to lock the list
before using it, you might forget to unlock the list while done and
you might deadlock against other list functions that you didn't know
were blocking too (or because you thought external locks were needed
there as well).
The short answer is: I don't know. Since nobody has replied though I
might as well share my thoughts on the matter.

Let's call those other three functions f(), g(), and h(). Does your
design allow f() to be called by one thread while g() is called by
another? If f(), g(), and h() all potentially modify the data
structure they should all lock together.

Assuming you've already handled that, then you can have an iterator
handle the locking. Let's say you decide that while one thread is
iterating through the list, no other thread can access the data
structure. Then, you can have the iterator's constructor lock the
structure, and have the iterator's destructor unlock it. If you do
this though, you have to worry about the copy semantics of the
iterator. I think auto_ptr provides a good analog for this. You also
have to assume that the client-programmer has the good sense not to
hang on to an iterator.

You could also have the iterator just lock on access and increment,
but then you have to worry about the iterator going bad mid-
iteration. Perhaps the iterator could keep the pointed-to item cached
internally to prevent this, and throw an exception if it gets
incremented after another thread just cleared the list or something.

In any event, I don't envy you a bit.

Regards,
Mark McKenna
Sep 12 '08 #2
an************* ****@gmail.com kirjutas:
I'm implementing some different c++ classes which I want to be thread
safe. All these classes contain lists or arrays of some kind.
I'm using protected sections to make them thread safe.

The question then is: how do you in a nice safe way pick values out of
the list? The easiest way is to have a pair of Lock, Unlock functions,
but this also presents a lot of ways of doing a misstake.
Right. If I understand correctly one of the requirements is that the
list/array should not mutate while taking out different items. Then there
are some solutions, which might or might not work for you (most probably
there are other solutions):

1) Add a function returning a copy of the list or array. After the
function has returned you can iterate and process items without locking.
This approach reduces thread contention and may thus give the best
overall performance, especially on multicore systems.

2) Add a scanning function which takes a functor as an argument. The
scanning function locks the array/list and then calls the callback
functor for each item. For supporting any functor the scanning function
should either be a template function, or the functor should be
polymorphic.

hth
Paavo
Sep 13 '08 #3
On Sep 12, 9:52 am, andreas.zetters t...@gmail.com wrote:
I'm implementing some different c++ classes which I want to be
thread safe. All these classes contain lists or arrays of some
kind. I'm using protected sections to make them thread safe.
Just to be 100% clear: what do you mean by "protected" sections?
("Protected" has a very definite meaning in C++, which has
nothing to do with threading. I presume you mean something to
do with locks or mutexs. The word "synchronizatio n" would be a
better choice in that case, if only because it avoids the
ambiguity with the C++ concept.)
The question then is: how do you in a nice safe way pick
values out of the list?
What do you mean by "pick values out of the list"? You need a
lock to read or access the values in the list, at least if there
is a thread anywhere else which may access the list. The
simplest rule is to never allow a pointer, a reference or an
iterator to something in the list to escape a synchronized
block. This is extrodinarily constraining, but the actual rules
are complex (and depend on the container).
The easiest way is to have a pair of Lock, Unlock functions,
but this also presents a lot of ways of doing a misstake.
Say the list class has 5 functions, one to get the number of
items, one to get items one by one and 3 other functions. The
3 functions have their locks internally as per c++ design you
don't want other classes need to know anything about internal
structure of another class. The 2 other functions is the
problem. I would have to have external locks to lock them as
the number of items might change while going through the list
otherwise. 3 problems arise with that solution: you might
forget (or don't know that it's needed) to lock the list
before using it, you might forget to unlock the list while
done and you might deadlock against other list functions that
you didn't know were blocking too (or because you thought
external locks were needed there as well).
There are two distinct problems here. The first is accessing
individual members in the container. You can't return a
reference or a raw pointer to the element, since this would
allow unlocked access to it; in addition, some other actions on
the container (in another thread) might invalidate the pointer
or reference. For read only access, returning a copy is often a
valid solution. I've also used smart pointers in this case:
return a reference counting smart pointer to the element, which
frees the lock when the last reference disappears. (You can use
boost::shared_p tr for this---just provide an appropriate
deleter.)

The second problem is race conditions outside of the container.
The size() function returns the number of elements at the moment
it is called, but this can change at any point outside of the
function. The granularity of the locks the container can
provide is too low to be really useful. (This is, of course,
why the standard containers don't lock.) You could provide a
nested class which manages some sort of scoped lock for the
container, and require the client code to use this. If you
wanted to be double sure, you could even have the scoped lock
inform the container of its existance, and which thread held it,
and assert() in each function that it was called by this thread.
How to solve this in a nice way? I've been thinking of perhaps
supplying a list to a special function in the list class that then
transfers all the list data in the class to the supplied list and thus
having a copy that won't risk changing. This will take double the
memory and will require an extra loop through the data though.
There are really only two choices if client code needs to call
more than one function with a consistent state: either the
client code uses a private copy, or it holds a lock for the
entire duration. Since only the client code can know the
appropriate granularity of locking, only the client code can
manage the locks. There are very, very few cases where it makes
any sense to have the container itself manage locking.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 14 '08 #4

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

Similar topics

5
1635
by: Med | last post by:
Hi, Could someone please tell me if I undrestand the concept of "Thread Saftey" correctly in Multi-threaded web application (asp.net 2) using following examples: 1. Class A is NOT a thread safe class: public sealed class A
3
2530
by: tcomer | last post by:
Hello! I'm working on an asynchronous network application that uses multiple threads to do it's work. I have a ChatClient class that handles the basic functionality of connecting to a server and sending/ receiving messages. The problem is, some of the ChatClient methods access another ChatWindow class that is derived from a Form and that causes some methods in ChatClient to required an Invoke(). My question is: is it possible to make...
15
2767
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
1
4617
by: jecheney | last post by:
Hi, Im currently using the following code for reading/writing to a network socket. private StreamReader clientStreamReader; private StreamWriter clientStreamWriter; .... TcpClient tcpClient = new TcpClient(server_host_name, server_port);
1
4613
by: =?Utf-8?B?SkE=?= | last post by:
I use a method for threading that instantiates an object that is a wrapper to a DLL (written in C). The wrapper class is passed a byte array, and then does GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + buffer.Length-1); It passes ptr to the DLL function FunctionX().
12
1678
by: Peter K | last post by:
Say I have this class public class Simple { private string name; public string Name { get { return name; } set { name = value; }
13
11449
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
29
9113
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: --Thread 1-- 1. Reads TCPIP Buffer 2. Adds Buffer to Queue (q.size() to check queue isn't full, and q.push_back(...)) 3. Signals Reading Thread Event & Goes Back to Wait for More Messages on TCPIP
5
3867
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The question then is: how do you in a nice safe way pick values out of the list? The easiest way is to have a pair of Lock, Unlock functions, but this also presents a lot of ways of doing a misstake. Say the list class has 5 functions, one to get the...
0
8746
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
8525
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
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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
6179
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
4175
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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
2
1737
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.