how to implement barrier?
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: how to implement barrier?

1. Junior Member
Join Date
Sep 2009
Posts
3

## how to implement barrier?

hello all...

i need help with some barrier... The idea of program is parrallel red/black SOR method (if smbdy knows what i am talking about), which is doing some calculations in matrix(matrix[i][j])... we have 2 phases, red and black... both are calculating (diferent) points in matrix, red is calculating if(i+j%2=0--->red points), bla bla and other ones if(i+j%2=0--->black points) ... main point is that they calculate different points...
i need to make that parrallel... so i have an idea to make just 2 threads, first 1 is calculating just points in upper half of matrix (red and black just barier between) and second lower half of matrix (again both red and black points). That would make program quicker, but biggest problem is that first whole red points have to be calculated (need to have barier, so both threads finish) and than black ones start to calculate and so on...

down is some kind of pseudo code for help...

my program is like this:

class calculations{

calculations1a() {...}

calculations1b() {...}

calculations2a() {...}

calculations2b() {...} // all 4 similar methods

//+other methods...

main(){ //this is whole text in main... just starting both threads...

}

run(){

// 1 is red phase, a is upper half of matrix
// 2 is black phase, b is lower half of matrix

calculations.calculations1a(); //thread1 calculates upper half of red points(need to wait calculations1b, that is in thread2 and than continue for calculations2a), same for other methods

BARRIER(); //need to wait for calculations1b --> so all red points r calculated

calculations.calculations2a();

}
}

run(){

calculations.calculations1b();

BARRIER();

calculations.calculations2b();
}
}

i hope any1 will understain my problem, if u need any more details or have some questions plz ask...

thanx

2. Member +
Join Date
Feb 2008
Posts
966

## Re: how to implement barrier?

What is your actual question? You just posted your pseudo code and a vague description of the problem. We here are not going to write the method implementations for you, so do you have any code for those methods yet? Are you having problems deciding where to start?

3. Member
Join Date
Aug 2009
Posts
44

## Re: how to implement barrier?

So far all i can see is a red and white board based with each squares colour based on whther a number is odd or not? So what are you doing with it? Scratch that ill just observe i get it now

4. Junior Member
Join Date
Sep 2009
Posts
3

## Re: how to implement barrier?

well guyz my question is how to implement barrier that will od this:

IMPORTANT THING is that 1st method and 3rd method need to finish and when they both finish 2nd method and 4th method can start execution... (look down how methods r set...)

do 1st method (anythin-doesnt matter)

BARIER (wait till 1st and 3rd finish)

do 2nd method (anything- doesnt matter)

BARIER(wait till 2nd and 4th finish)

recursive call of 1st thread again

do 3rd method (anythin-doesnt matter)

BARIER (wait till 1st and 3rd finish)

do 4th method (anything- doesnt matter)

BARIER(wait till 2nd and 4th finish)

recursive call of 2nd thread again

all methods are writen already but i didnt want to complicate things up....

i just need idea how to make this barrier...

i wrote those thing abput red/black points cuz i thought u will understain problem easier...

thanx

5. Member +
Join Date
Feb 2008
Posts
966

## Re: how to implement barrier?

By barrier are you trying to say that you need to make the matrix synchronized so that multiple threads don't overwrite each others work while performing actions on it?

6. Junior Member
Join Date
Sep 2009
Posts
3

## Re: how to implement barrier?

exactly that

7. Elite Member Power Poster
Join Date
May 2006
Location
UK
Posts
4,473

## Re: how to implement barrier?

How about using a java.util.concurrent.CyclicBarrier

8. Member +
Join Date
Feb 2008
Posts
966

## Re: how to implement barrier?

keang has suggested using a class that is designed to be thread safe (hence it being in the concurrent package).

As a matter of fact, if you look at the api for this class, it gives an example of exactly what you are trying to do (well, almost exactly).

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width