Re: [Dev-C++] making data structures thread safe
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
|