ArticleslgStudy

computer science

Barrier (computer science)

Barrier (computer science) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Barrier (computer science) rather than just read about it. In short: In parallel computing, a barrier is a synchronization method. A barrier for a group of threads or processes in the source code means that all thread/process stop at that point and do not proceed until all other threads/processes reach this barrier.

Key takeaways

  • Barrier (computer science) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Barrier (computer science) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Barrier (computer science) from memory before moving on to harder problems.

Reference excerpt

In parallel computing, a barrier is a synchronization method. A barrier for a group of threads or processes in the source code means that all thread/process stop at that point and do not proceed until all other threads/processes reach this barrier. Many collective routines and directive-based parallel languages impose implicit barriers. For example, a parallel do loop in Fortran with OpenMP will not be allowed to continue on any thread until the last iteration is completed. This is in case the program relies on the result of the loop immediately after its completion. In message passing, any global communication (such as reduction or scatter) may imply a barrier. In concurrent computing, a barrier may be in a "raised" or "lowered state". The term "latch" is sometimes used to refer to a barrier that starts in the raised state and cannot be re-raised once it is in the lowered state. The term "count-down latch" is sometimes used to refer to a latch that is automatically lowered once a predetermined number of threads/processes have arrived.

Implementation Consider the following example of a thread barrier. The thread barrier requires a variable to "keep track of the total number of threads that have entered the barrier. Whenever sufficient threads enter the barrier, it will be lifted. A synchronization primitive like a mutex is also needed when implementing the thread barrier. This thread barrier method is also known as a "centralized barrier", as the threads wait before a "central barrier" until the expected number of threads have reached the barrier before it is lifted. This can be demonstrated by the following C example using POSIX threads.

In this program, the thread barrier, struct Barrier, consists of:

lock: A POSIX thread mutex lock thread_count: Total threads in the process barrier_count: Total number of threads expected to enter the thread barrier so that it can be lifted Based on the definition of barrier, the implementation requires a function like thread_barrier_wait() in this program which "monitors" the total number of thread in the program in order to life the barrier. In this program, every thread calls thread_barrier_wait() will be blocked until THREAD_BARRIERS_NUMBER threads reach the thread barrier. As the main thread is blocked due to not having three threads, the line "Thread barrier is lifted" is never reached. The result of that program is:

Only two threads are created with thread_func() as the thread function handler, which calls thread_barrier_wait(&barrier), while the thread barrier expects three threads to call thread_barrier_wait() in order to be lifted. Upon changin TOTAL_THREADS to 3, the thread barrier is lifted:

Sense-reversal centralized barrier Besides decrementing the total thread number for each thread successfully passing the thread barrier, thread barriers may use opposite values to mark each thread state as passing or stopping. For example, 0 may denote stopping at the barrier while 1 denotes passing the barrier. This is known as "sense reversal", as demonstrated by the following:

This version of the previous centralized barrier implementation introduces two new variables:

local_flag: A thread-local Boolean to check whether THREAD_BARRIERS_NUMBER threads have arrived at the barrier. flag: A Boolean flag in struct Barrier indicating whether THREAD_BARRIERS_NUMBER threads have arrived at the barrier. When a thread stops at the barrier, local_flag's value is flipped. When there are less than THREAD_BARRIERS_NUMBER threads stopping at the thread barrier, those threads will keep waiting with the condition that barrier.flag is not equal to local_flag. When there are exactly THREAD_BARRIERS_NUMBER threads stopping at the thread barrier, thread_count is reset to 0 and flag is set to local_flag.

Combining tree barrier A potential problem with the centralized barrier implementation is that because all threads repeatedly access the global variable to pass/stop, communication traffic is high, decreasing scalability. This problem can be resolved by regrouping the threads and using multi-level barrier, such as using a combining tree barrier. Hardware implementations may have greater scalability. A combining tree barrier is a hierarchical way of implementing barrier to resolve scalability by avoiding the case that all threads are spinning at the same location. In a k-tree barrier, all threads are equally divided into subgroups of k threads and a first round of synchronizations are done within these subgroups. Once all subgroups have completed synchronization, the first thread in each subgroup enters a second level for further synchronization. In the second level, like in the first level, the threads form new subgroups of k threads and synchronize within groups, sending out one thread in each subgroup to next level and so on, until in the final level, only one subgroup needs to be synchronized. After the final level of synchronization, the releasing signal is transmitted to upper levels and all threads pass the barrier.

Hardware barrier implementation A hardware barrier uses hardware to implement the above basic barrier model. The simplest hardware implementation uses dedicated wires to transmit signals to implement a barrier. This dedicated wire performs OR/AND operations to act as pass/block flags and thread counters. For small systems, such a model is sufficient and communication speed is not a major concern. In large multiprocessor systems, this hardware design can lead to high latency in the barrier implementation. The network connection among processors is one means to lower latency, analogous to using a combining tree barrier.

POSIX Threads barrier functions The POSIX Threads standard directly supports thread barrier functions which can be used to block the specified threads or the whole process at the barrier until other threads to reach that barrier. The POSIX Threads offer three main APIs:

pthread_barrier_init(): Initializes the thread barrier with the number of threads necessary to wait at the barrier to lift it. pthread_barrier_destroy(): Destroys the thread barrier and releases the resource. pthread_barrier_wait(): Blocks the current thread until the number of threads specified by pthread_barrier_init() calls pthread_barrier_wait() to lift the barrier. The following example in C using POSIX Threads uses thread barriers to block all the threads of the main process, thus blocking the whole process.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Barrier (computer science)

Start with the simplest possible case. Write down what Barrier (computer science) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Barrier (computer science) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Barrier (computer science) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Barrier (computer science)

In research
Barrier (computer science) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Barrier (computer science) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Barrier (computer science) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrency (computer science), Concurrency control, Parallel computing, so understanding it makes those chapters shorter.
In everyday life
Look for Barrier (computer science) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Barrier (computer science)” →

How to study Barrier (computer science) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Barrier (computer science) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Barrier (computer science) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Barrier (computer science) in simple terms?

In parallel computing, a barrier is a synchronization method. A barrier for a group of threads or processes in the source code means that all thread/process stop at that point and do not proceed until all other threads/processes reach this barrier.

Why does Barrier (computer science) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Barrier (computer science)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Barrier (computer science).

Tags

  • Concurrency (computer science)
  • Concurrency control
  • Parallel computing
  • Synchronization

Keep exploring