I am new to TBB. I am using the parallel_for and am running into problems when using it in a simple example i made.
When I run the code in serial mode by commenting out the parallel section, the variable count is (10 to the power 10) as should be and variable count1 is (10 to the power5). When i set grainsize to 10,000 or 50,000 and run it in the parallel mode, my count is less than (10 to the power 10) and count1 is less than (10 to the power 5). The variable count should always be equal to (10 to the power 10) always because the function Foo should update it that many times. It shoulnt matter whether I am running in parallel or serial mode. If I set the grainsize for my parallel_for to 100,000 or above, my count and count1 are good.(I guess its just running in a single processor if i set grainsize equal to 100,000 as the no of iterations is equal to 100,000)
What am I doing wrong? Please help! I cant understand this.
using namespace tbb;
using namespace std;
static int count;
static int count1;
float *const my_a;
void operator()(const blocked_range
void SerialApply(float s,size_t n)
static void Foo( float g)
int main(int argc, _TCHAR* argv)
cout<<"\n"<<<"\n "<<<" \n";
One of the tricky bits of parallel programming is that with multiple agents simultaneously updating things in memory, programmers need to guard against the possibility that multiple threads may try to change the same things at the same time, a so-called race condition.
The variables count and count1 may have this problem in the code sample you shared. Both function Foo and the ClassFoo functor will have multiple copies executing simultaneously, so updates to them need to be guarded, either by using a critical section around each of the increments or in this case declaring each of them atomic. You might learn more about races here.