M
Manoj
How do I prevent local variable value in a thread function, its
changing if thread function call several times.
e.g.
void ThreadCS(void* lp)
{
EnterCriticalSection(&cs);
const string str = string((char*) lp);
int nNum = atoi(str.c_str());
for(int i=1; i <=10; i++)
{
printf("\t%s - %d\n",str.c_str(), i); //here str value is changed I
want to keep same in single call
Sleep(100);
}
LeaveCriticalSection(&cs);
SetEvent(ghEvents[nNum]);
}
int _tmain(int argc, _TCHAR* argv[])
{
......
......
for(int i=0; i<numThreads; i++)
{
sprintf(szStr,"%d",i);
_beginthread(ThreadCS, 0, szStr);
}
......
......
}
here is the out put of this
2 - 1
2 - 2
2 - 3
2 - 4
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
I want it should display in this way
1 - 1
1 - 2
1 - 3
1 - 4
1 - 5
1 - 6
1 - 7
1 - 8
1 - 9
1 - 10
2 - 1
2 - 2
2 - 3
2 - 4
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
3 - 1
3 - 2
3 - 3
3 - 4
3 - 5
3 - 6
3 - 7
3 - 8
3 - 9
3 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
Any clue ?
changing if thread function call several times.
e.g.
void ThreadCS(void* lp)
{
EnterCriticalSection(&cs);
const string str = string((char*) lp);
int nNum = atoi(str.c_str());
for(int i=1; i <=10; i++)
{
printf("\t%s - %d\n",str.c_str(), i); //here str value is changed I
want to keep same in single call
Sleep(100);
}
LeaveCriticalSection(&cs);
SetEvent(ghEvents[nNum]);
}
int _tmain(int argc, _TCHAR* argv[])
{
......
......
for(int i=0; i<numThreads; i++)
{
sprintf(szStr,"%d",i);
_beginthread(ThreadCS, 0, szStr);
}
......
......
}
here is the out put of this
2 - 1
2 - 2
2 - 3
2 - 4
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
I want it should display in this way
1 - 1
1 - 2
1 - 3
1 - 4
1 - 5
1 - 6
1 - 7
1 - 8
1 - 9
1 - 10
2 - 1
2 - 2
2 - 3
2 - 4
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
3 - 1
3 - 2
3 - 3
3 - 4
3 - 5
3 - 6
3 - 7
3 - 8
3 - 9
3 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
4 - 1
4 - 2
4 - 3
4 - 4
4 - 5
4 - 6
4 - 7
4 - 8
4 - 9
4 - 10
Any clue ?