hello
how do i make both threads have access to the same set of updated data, when they call different functions?
in addition, is the way i pass in the variable "myObjs" correct? can i pass in STL containers the same way?
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
...
class aClass{
...
};
int main(){
void *plus (void *);
void *minus (void *);
pthread_t thread1, thread2;
...
queue<aClass> myObjs;
...
int size = myObjs.size(); //this is OK.
retVal1 = pthread_create(&thread1, NULL, plus, (void *) myObjs);
retVal2 = pthread_create(&thread2, NULL, minus, (void *) myObjs);
...
}
...
void *plus(void *ptr){
queue<aClass> *myObjs = (vector<aClass>*) *ptr;
int size = myObjs.size(); //generates error. how do i resolve this?
}
|
in this code, thread1 will increase the value of member variables, while thread2 will decrease the value of the member variables... but i need them to be updated.
in addition, when im adding the values, how do i mutex lock the minus function, so it wont add and minus at the same time, and vice versa?
if i change the code to use STL container, i would edit the function "plus()" to add a new object, and the function "minus()" to erase an object.
lastly, how do i use the functions in queue, and the class? it generates an error when it's in the thread function.
the error is:
error: request for member 'size' in 'myObjs', which is non class type 'std::queue<aClass, std:: deque<aClass, std::allocator <aClass>>>*'