multithreading problem

Hi all,
I have's an example of what I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vector<int> global_vector;

int Thread(void*){
    cin.get();
    global_vector.push_back(15);//lets say, that vector will be reallocated now
    return 0;
}

int main(){
    //CreateThread
    vector<int>::iterator i;
    while(true){
        for(i = global_vector.begin(); i != global_vector.end(); i++){
            cout<<*i;//The app will crash here because
            //global_vector gets reallocated and i doesn't
            //point where it should any more
        }
    }
    return 0;
}

What could I do with this? The only thing that comes to head is some sort of messaging system, though that seems a needlessly complex solution for a simple problem...
Last edited on
vectors are not threadsafe. Even if that push_back call doesn't reallocate, this could cause other weird problems.

You could use an intermediary buffer instead of adding to the vector directly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
list<int> intermediate;  // deque or list since we need FIFO

int Thread(void*){
  LockMutex( mutex );
  intermediate.push_back(15);
  UnlockMutex( mutex );
}

int main()
{
  ...
  while(true)
  {
    for(...) { }

    LockMutex( mutex );
    while(!intermediate.empty())
    {
      global_vector.push_back( intermediate.front() );
      intermediate.pop_front();
    }
    UnlockMutex( mutex );
  }
}


You could put that push_back/pop_front loop inside the for, but then you'd have to use integer indexes and not iterators for the loop.

EDIT: don't forget the mutexes!
Last edited on
Oh thanks, I didn't know about mutexes before.
Though I can't find LockMutex on msdn? (only CreateMutex) What library is it in?

Also, doesn't using mutexes solve the whole problem? I mean, something like
1
2
3
4
5
while(true){
    LockMutex(mutex);
    for(...)//do stuff with vector
    UnlockMutex(mutex);
}
wouldn't let the thread change the vector while there are any active iterators.
Last edited on
Though I can't find LockMutex on msdn? (only CreateMutex) In what library is it?


I don't know if it is an actual function. I just made it up (pseudocode).

There's definitely a way to lock mutexes though, it depends on whatever lib you're using. The CreateMutex documentation should have a link to related functions in the "See Also" section.

Also, doesn't using mutexes solve the whole problem?


Yeah. x_x I don't know why I did that the hard way, but yeah you're right. Heh.
Though I can't find LockMutex on msdn? (only CreateMutex) What library is it in?

It should look like this:

1
2
3
4
5
6
7
8
9
HANDLE mutex = CreateMutex(0,false,0); // initialize once.

// use this around every critical section
WaitForSingleObject(mutex, INFINITE);
...
ReleaseMutex(mutex);


CloseHandle(muteX); // the typical free stuff... 


Don't give the mutex a name or else it will be a system-wide inter-process lock.


Instead of the Windows-Functions, you can have a look at boost::thread, which provides mutexes too and will be the API that will come with the standard library some day, so you already preparing ;-)

Ciao, Imi.
If you're locking within a process, use Critical Sections instead of Mutex as it's faster.

I'd consider this required reading.
http://msdn.microsoft.com/en-us/library/ms686364%28VS.85%29.aspx
Topic archived. No new replies allowed.