0

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:

...

class aClass{
...
};

int main(){
    void *plus (void *);
    void *minus (void *);
    pthread_t thread1, thread2;
    ...
    aClass[100] myObjs; // may use STL Containers
    retVal1 = pthread_create(&thread1, NULL, plus, (void *) myObjs);
    retVal2 = pthread_create(&thread2, NULL, minus, (void *) myObjs);
    ...
}
...

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?

lastly, 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.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

>>how do i make both threads have access to the same set of updated data, when they call different functions?

Look up the term "thread synchronization". Use a semiphore to lock one thread out while another thread is using the object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.