1

I am having a hard time to figure out what can be a well-organized and efficient design for the following objects that I have as part of a bigger project.

Please note that I am having multiple threads working on the objects, some reading, and others writing into the objects.

I have a large stl vector of objects let's say Nodes. Each node has some attributes plus 2 small stl vectors. Threads may modify objects and the two vectors inside them.

I know stl vectors are not thread safe, and I have to provide it, but I think it's easier to use intel TBB rather than stl vector for the large vector. I tried this but I'm getting segmentation faults as threads may modify the two smaller vectors inside Nodes, as they can be stored outside of the Node's space and therefore TBB's concurrent_vector does not protect them, even though they are attributes of the Node.(Please correct me if I'm wrong.) Now I am thinking to removing the large stl vector and keep nodes as just free nodes in the heap. At least this way I can better provide synchronization. But I doubt I can make it as fast as intel TBB. I cannot use intel TBB's vector anyway, because it does not protect the small internal vectors inside Nodes. (For some reason I have to keep the internal vectors as stl vectors).

Now my question is that "Is this a good and efficient design?" Any comment is appreciated.

Class All
{
 tbb::concurrent_vector<Node> MyArray;
}

Class Node
{
    void inline Check_Set_lock ();
    void inline Unset_lock ();

    std::vector<pair<int,int> > MBR;    
    std::vector<double> Measures;
    omp_lock_t xlock;
}

Please note I have to use an openmp lock plus two lock/unlock methods to protect Node objects. I also know about Boost threads, but here again I am having a hard time if I should choose Boost threads to provide separated read and write locks? I think openmp does not provide shared locks.

My last question is can I only use the xlock in the code to protect all attributes and the stl vectors inside each Node when it's being read/written by a thread? I think this will degrade the parallelism, but what would be a better choice?

4

0 回答 0