Home > Archive > VC Language > January 2006 > Thread safe vector class
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Thread safe vector class
|
|
| Jim Langston 2006-01-29, 9:57 pm |
| I'm looking for a thread safe vector class for windows. Using Microsoft
Visual C++ .net 2003.
If I can't find one I'll have to make and and it's a little bit beyond me
right now, but I'm plugging away at it anyway.
Anyone know of one?
| |
| Carl Daniel [VC++ MVP] 2006-01-29, 9:57 pm |
| Jim Langston wrote:
> I'm looking for a thread safe vector class for windows. Using
> Microsoft Visual C++ .net 2003.
Thread safe for what kinds of simultaneous access?
-cd
| |
| Heinz Ozwirk 2006-01-30, 4:00 am |
| "Jim Langston" <tazmaster@rocketmail.com> schrieb im Newsbeitrag
news:0%dDf.332$Q%3.5@fe02.lga...
> I'm looking for a thread safe vector class for windows. Using Microsoft
> Visual C++ .net 2003.
Good luck! Alas, thread safety is too complex to implement for a general
vector (or any other container) implementation. Perhaps it might be
possible, but such a thread safe vector would probably have too much
overhead to be really usefull. How safe should it be?
Would it be safe enough to synchronize access to a single element, such that
simple statements like
Thread A { a = vector[i]; }
Thread B { vector[i] = b; }
? That would be easy, but that would not help for statements like
vector[i] = 2 * vector[i] + 1;
Of cause, that problem could also be solved somehow, but only for the cost
of some overhead in time and space, and it wouldn't help with simple code
like
int sum = 0;
for (int i = 0; i < vector.size(); ++i)
{
sum += vector[i];
}
And I am afraid that this cannot be done with only a new user defined type.
At this point you'll probably need the help of your compiler.
For most purposes it is much easier to specify the operations you really
need for your data collections and implenment these operations in a thread
safe manner.
HTH
Heinz
| |
| Jim Langston 2006-01-30, 7:07 pm |
| "Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:0%dDf.332$Q%3.5@fe02.lga...
> I'm looking for a thread safe vector class for windows. Using Microsoft
> Visual C++ .net 2003.
>
> If I can't find one I'll have to make and and it's a little bit beyond me
> right now, but I'm plugging away at it anyway.
>
> Anyone know of one?
It seems I need to provide more detail with what I'm looking for since 2
people asked.
The specific case I want it for right now is for a message queue between
worker threads and the main thread. It would be one way communication, that
is, the worker threads add messages (std::strings) with push_back() and the
main thread retrieves the messages.
This would seem to be rather trivial just by wrapping a vector in a class
and having the class only give access to the specific methods needed for
this queue. push_back, [] (or is that (), not sure what to overload for [])
and a few others.
But then I was thinking if I go ahead and do this, I might as well make it
usable for all data types, meaning it now needs to become a template rather
than a wrapper class. Which is no longer trivial. So I was hoping someone
already wrote one.
| |
| Carl Daniel [VC++ MVP] 2006-01-30, 7:07 pm |
| Jim Langston wrote:
> "Jim Langston" <tazmaster@rocketmail.com> wrote in message
> news:0%dDf.332$Q%3.5@fe02.lga...
>
> It seems I need to provide more detail with what I'm looking for
> since 2 people asked.
>
> The specific case I want it for right now is for a message queue
> between worker threads and the main thread. It would be one way
> communication, that is, the worker threads add messages
> (std::strings) with push_back() and the main thread retrieves the
> messages.
> This would seem to be rather trivial just by wrapping a vector in a
> class and having the class only give access to the specific methods
> needed for this queue. push_back, [] (or is that (), not sure what
> to overload for []) and a few others.
>
> But then I was thinking if I go ahead and do this, I might as well
> make it usable for all data types, meaning it now needs to become a
> template rather than a wrapper class. Which is no longer trivial. So I
> was hoping someone already wrote one.
OK, what you're looking for isn't a threadsafe vector, but a
producer/consumer queue (which might be built internally on a vector).
Try this one out:
http://www.codeproject.com/threads/semaphores.asp
If that one doesn't fit your needs, Google for "thread safe producer
consumer queue" and you'll get dozens of hits.
-cd
|
|
|
|
|