Thread: [Dev-C++] making data structures thread safe
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Siva C. <siv...@ya...> - 2007-10-04 07:48:17
|
Hello all,
I am trying to understand on how to make a data
structure thread safe. I will use a simple example to
explain my problem.
Consider a class Array which encapsulates a 1d array
with the following definition:
<code>
class Array
{
public:
...
...
int& operator[](int index);
...
...
};
Array A(10); // a 10 element array
</code>
This array A can be accessed by two different threads
simultaneously - one thread could be reading a[5], and
another writing a[5]! How to protect my array from
such read/write conflicts? I am looking for a solution
which enables a user of the array class not to bother
about such conflicts. Is such a thing possible? If
yes, what is it?
Thanks and regards,
Siva Chandra
____________________________________________________________________________________
Check out the hottest 2008 models today at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html
|
|
From: Jonathan W. <jon...@gm...> - 2007-10-04 18:18:06
|
Hi,
You will need to use a mutex (as in mutual exclusion). Only one 'person' can
have control of it at one time.
Your operators should all try to acquire the lock on the mutex before
doing anything, and release it once they are done. You should also
take care to not segfault while holding the lock, or you will never
release it.
I am not familiar with mutexes in C++, only Java so I cannot tell you what
class to use but I'm sure a quick google will yield satisfactory pointers.
You might also want to provide lock/release methods for the whole Array, in
case a thread wants to have exclusive access to the array itsself. (eg. for
sorting, etc)
Cheers,
Jonathan
On 10/4/07, Siva Chandra <siv...@ya...> wrote:
>
> Hello all,
>
> I am trying to understand on how to make a data
> structure thread safe. I will use a simple example to
> explain my problem.
>
> Consider a class Array which encapsulates a 1d array
> with the following definition:
>
> <code>
> class Array
> {
> public:
> ...
> ...
> int& operator[](int index);
> ...
> ...
> };
>
> Array A(10); // a 10 element array
> </code>
>
> This array A can be accessed by two different threads
> simultaneously - one thread could be reading a[5], and
> another writing a[5]! How to protect my array from
> such read/write conflicts? I am looking for a solution
> which enables a user of the array class not to bother
> about such conflicts. Is such a thing possible? If
> yes, what is it?
>
> Thanks and regards,
> Siva Chandra
>
>
>
> ____________________________________________________________________________________
> Check out the hottest 2008 models today at Yahoo! Autos.
> http://autos.yahoo.com/new_cars.html
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems? Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >> http://get.splunk.com/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
--
<Morpheus> linux, c'est une question de VI ou de MORE
|
|
From: Chris M. <lor...@gm...> - 2007-10-04 21:59:22
|
On 10/4/07, Siva Chandra <siv...@ya...> wrote: > Hello all, snip! > This array A can be accessed by two different threads > simultaneously - one thread could be reading a[5], and > another writing a[5]! How to protect my array from > such read/write conflicts? I am looking for a solution > which enables a user of the array class not to bother > about such conflicts. Is such a thing possible? If > yes, what is it? I have no idea, really, however, I do know that it was either the Boost C++ Libraries or the STL which gives you smart pointers, which automatically deallocate their memory when there are no more references to them in scope. I think it could be possible to implement a similar technique to memory locking to prevent thread collisions. However, I've never done anything like that in C++, and am just going out on a limb here. That's the sort of approach I'd take assuming that it's of the utmost importance that the user of the class need not pay any attention to thread locking/unlocking and rubbish of that sort. -- Registered Linux Addict #431495 If You Know What's Good For You, You'll Install Linux John 3:16! |
|
From: Jan M. <jan...@vo...> - 2007-10-07 13:49:49
|
Hello, I would like to know if someone here is familiar with pcre.h? And if you know where can I find the version for DOS or Windows? Thank you Jan Mura jan...@vo... |
|
From: Reid T. <rei...@at...> - 2007-10-07 17:26:26
|
Jan Mura wrote: > Hello, > > I would like to know if someone here is familiar with pcre.h? > And if you know where can I find the version for DOS or Windows? > > Thank you > > Jan Mura > jan...@vo... > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users here http://www.pcre.org/ might help |