Reposting this since my prev question got closed..I've tried to explain it better this time...pls let me know if u need further clarifications:
I have a class A with a function doSomething(int count) Within class A, I want to open 100 threads each of which call the function doSomething() and pass in the count 1-100 in each thread.
Which means...when the 1st thread calls this function, it should call doSomething(1), 2nd thread should call doSomething(2) and so on...
This is what my code looks like:
struct input {
A* in;
int count;
};
myFunc(void* data)
{
input* tP = (input*) data;
A* obj = tP->in;
int ct = tP->count;
obj->doSomething(ct);
}
class A {
doSomething(int count);
Thread2doSomething();
}
doSomething(int count)
{
cout<<"Print value is"<<count;
}
Thread2doSomething()
{
for (i = 1 to 100)
{
input myIN;
myIN.in = this;
myIN.count = i;
beginthreadex(myFunc, &myIN);
}
}
I expect that the above code will spawn 100 threads here..each of which will have a new value of count 1,2,3...100 when it calls doSomething();
each invocation of doSomething on a new thread should have a different value of count passed to it - 1,2,3,...to 100.
But that does not happen. The count values passed to it are pretty random...often it gets the same value multiple times...and does not get some values at all.Sometimes the value of count passed to doSomething is the same in all the threads...
the calls looks more like this: doSOmething(4),doSomething(4),doSomething(7),doSomething(10),doSomething(10) and so on.
Hope I've clarified things...pls advise.