0

Hi,

I've searched far and wide but I haven't found exactly what I need.

I have to programs communicating through named pipelines (windows XP). One is sending strings to another. The one who's recieves inserts them into an array. Now, here's the problem: this program is multi-threaded, each thread has it's own class, and I need to make the string array visible to all the threads. So I thougth that I could pass the string array reference to the other threads so when I change it in the main thread, every other thread will see it too, but, only the last value of the array is updated! Here it is in pseudocode:

int main(){

string * strArray= new string[10];

ThreadObject obj;

obj.LoadArray(strArray);
int i=0;
          while(true){
                
                 string buff=RecieveFromPipe();
                 strArray[i]=buff;
                  i=(i+1)%10;

             }
}

The LoadArray function is prototyped as this:

void ThreadObj::LoadArray(string *a){

     string_pointer=a;

}

Any idea? I'm sorry if I'm not perfectly clear, English is not my first language and my choice of words is very limited.

Thanks in advance,

GGAB

2
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by GGAB
0

why the while loop? can't you just do something like

for(int i=0;i<10;++i) {
   strArray[i] = ReceiveFromPipe();
}

?

0

The while(true) statement is because this thread runs without end until I send a terminal signal, it is a detail unimportant to the problem, I supressed a lot of the code since it would bring more confusion. And even so, it is a work in progress and is likely to change.

The array is a circular one, it is aways recieving new strings, when a string is inserted in the strArray[9], the next one will be inserted in strArray[0] and so on.

When I print the string from the main thread (the one with the while(true)), it prints out just right, but when I print from another thread (the one with the loadArray method), all positions print blank, except the last one, and that's the part that puzzles me.

Any other way that I can explain my problem better please ask away, I really want to understand this problem, since for me there isn't a logical reason for this error to occur.

Thanx for the reply

0

I solved, empirically, the problem. The thing is that when you write a value in a position of the array, you have to yield to the other thread. If you don't, it will only recognize correctly the last value that you changed.

Although it is working, I would like a more scientifical explanation. If someone could help me with that it would be great.

GGAB

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.