multiple threads and vector, good objects in a bad crowd.

Started by
7 comments, last by griffenjam 22 years ago
I am writing a multithreaded server with a vector holding all the client info. However I am getting access violations when I try to read from the vector. The strange thing is that is ALWAYS happens in the same place in my code, but at another point in the same thread I read from the vector with no problem. I basically have a read thread, which reads in data from the socket, and a process thread (which process the data). Both threads loop through the vector with no problem. But in the process thread if you send a global message it has to loop through all vectors again, thats when the violation happens. Jason Mickela ICQ : 873518 E-Mail: jmickela@sbcglobal.net
Please excuse my spelling -"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Advertisement
Are you synchronizing your access to the vector? Are you making sure no thread has changed the vector while another is reading it?
From your description this is what I envision your doing:

SocketReaderThread{
while socket_open
Read the socket;
Add data to end of vector;
Signal ProcessThread
End loop;
}


ProcessThread{
For each entry in vector
process entry
end loop
}

Am I correct? If so I would just lock the vector whenever I''m writing to it(SocketReaderThread) and either lock the vector when you are reading from it(ProcessThread) or allow the ProcessThread to be interupted by the SocketReaderThread whenever new data arrives.
That''s not quite what I''m doing. i''m using a circular buffer to store the data, the vector mearly keeps track of the clients.
BTW, it''s a vector of pointers. Useally I do nothing but read from the vector, I only change it if someone connects or disconnects.

Not it gets wierd. I put in a bool that would be set to true whenever I would try to send a global message. That way the other processes wouldn''t access the vector while the bool was true. Not only did that not fix anything, but now my program crashes when it reaches the bool. I thought that maybe I REALLY suck at multithreaded programming, and I cant even read a variable that is being written to in another thread without crashing. So, I took the bool out of all the threads, but left it in where it is set to true. It STILL crashes when setting it to true, even though it isn''t being accessed from anywhere else.
I noticed that when I put a breakpoint on it and try to "Watch" the variable the message "Error: Expression cannot be evaluated" comes up in my watch window.
WTF?

Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net


Please excuse my spelling
-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Look into mutexes (right spelling for plural mutex?).
--------------------Go Stick Your Head In A Pig
Is the memory you are accessing thread local or in a shared memory area?

Which platform are you writing this code on?

Show us the relavant vector declaration line, and the access (violating) line of code. Also show us the line making the threads.
Lets see if I have it now.

You have a main thread that accepts clients and adds them to the vector, and spawns a socket reader thread.

Each socket reader thread puts data into the buffer.

While all this is going on the process thread is reading the buffer and handling the data. After it gets data from the buffer it deletes it?

My question is are you using any kind of synchronization at all? and what language are you using?
O.k. I just fixed it, kinda.

I don''t understand why what I did works though.

Ok here goes.
I have a main class that controls everything in the server.
It creates the process thread.
The process thread gets a pointer back to the server.
The process thread uses that pointer to iterate through each connected client and call a member of the server called ProcessClient.

looks like this

NetServer *server = (NetServer *) arg;
...
server->ProcessClient(client);
...
That works fine, but when I get inside ProcessClient, none of the members of NetServer are available. I can call other member functions, but the second I try to access a data mamber I get an access violation.
I changed the code so that I send a pointer to the NetServer class to ProcessClient and use that instead of trying to directly access members. It works. But why? Since ProcessClient was called with that same NetServer variable as it recieves shouldn''t the this pointer be the same?




Jason Mickela
ICQ : 873518
E-Mail: jmickela@sbcglobal.net


Please excuse my spelling
-"I''m Cloister the Stupid" Lister (Red Dwarf)
"The paths of glory lead but to the grave." - Thomas GrayMy Stupid BlogMy Online Photo Gallery
Using another variable is not going to cut it. Your going to need to use a CRITICAL_SECTION or a Mutex.

You can check out this thread which contains a few references.

You may also need to declare the vector as volatile.

[edited by - Solo on March 22, 2002 5:52:27 PM]
// Ryan

This topic is closed to new replies.

Advertisement