Local Variable in Thread

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 ?
 
S

SG

void ThreadCS(void* lp)
{
      EnterCriticalSection(&cs);
      const string str = string((char*) lp);
.....
}

int _tmain(int argc, _TCHAR* argv[])
{
.....
.....

char szStr[99];
      for(int i=0; i<numThreads; i++)
      {
          sprintf(szStr,"%d",i);
          _beginthread(ThreadCS, 0, szStr);
      }
.....
.....
}

Any clue ?

Give each thread a copy of the string instead of just a pointer. Using
a decent thread library (like Boost.Thread) it might look as simple as
this:

void my_thread_func(string const& s)
{
lock lck (cout_mutex);
cout << s << '\n';
}

int main() {
string foo = "hello";
thread t1 ( boost::bind(my_thread_func,foo) );
foo = "world";
thread t2 ( boost::bind(my_thread_func,foo) );
t1.join();
t2.join();
}

boost::bind yields a function object that stores a copy of foo in this
case. The thread's constructor stores a copy of the function object
somewhere safe, starts the thread and disposes the function object
automatically. But passing a reference to foo is also possible.
Replace foo within bind with cref(foo) where cref is a function
template from Boost.Ref (reference wrappers).

If for some reason you cannot use a decent thread library, you have to
dynamically create copies of the string, pass a pointer to the copy to
the thread and let the thread delete the copy again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top