Hi all,
I Just have just getting started to use intels Threading Building Blocks, but it just took some minutes before I got some problem. If I am running the code below will m_type.size() always return 0 even if i am pushing new data in a second thread:

But if I am making m_type to a reference (&) will it work without problems.

I know that local variables can be read into a processor cache and that is what I think happens, but how do I prevent it? And if it isn't the problem, what I am doing wrong then?

I second question:
Do any of you have any good multi threading links to share?

Thanks in advance :)

12-05-2009

C_ntua

Can you post the exact output of the program?

12-05-2009

idleman

Exactly? No, it is different every time I run it of course.

The program acts like it got 2 tbb::concurrent_vector<std::string> separated for each thread, even if it just got one. Even if the worker method pushes new data to the vector does the consumer never see it. It is like it is 2 different variables separated from each other. Worker thread says m_type.size() have size (x > 0) and consumer always 0. If I am in another way make m_type to a reference will it work, probably because the compiler cannot make some optimizes and if I am trying to make the variable of type volatile I just got compile errors.

I have search little and I probably need a memory barrier that force the processor to clean its L1/L2 cache and register every time, but I always have used boost::mutex before this so I don't really know how I fix it. I could add a mutex (mutex force memory barrier as I know), but what should be the point to use a concurrent container then?

12-05-2009

abachler

You need to use critical sections to prevent simultaneous access to a modifiable object.

12-05-2009

idleman

What do you mean? It is a concurrent container, it is thread safe to access from multiple threads, both read and write it says in the documentation.

12-06-2009

CornedBee

Give Test a private copy constructor, and you'll see the problem (or not, given bind's error messages): Boost.Bind binds its arguments as copies. In other words, each of these threads has its private Test object, with its own private vector. You need to bind by reference, which you can achieve by using Boost's reference_wrapper: