Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:51:01+00:00 2026-05-23T17:51:01+00:00

Is it safe to use __syncthreads() in a block where I have purposefully dropped

  • 0

Is it safe to use __syncthreads() in a block where I have purposefully dropped threads using return?

The documentation states that __syncthreads() must be called by every thread in the block or else it will lead to a deadlock, but in practice I have never experienced such behavior.

Sample code:

__global__ void kernel(float* data, size_t size) {
    // Drop excess threads if user put too many in kernel call.
    // After the return, there are `size` active threads.
    if (threadIdx.x >= size) {
        return;
    }

    // ... do some work ...

    __syncthreads(); // Is this safe?

    // For the rest of the kernel, we need to drop one excess thread
    // After the return, there are `size - 1` active threads
    if (threadIdx.x + 1 == size) {
        return;
    }

     // ... do more work ...

    __syncthreads(); // Is this safe?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:51:02+00:00Added an answer on May 23, 2026 at 5:51 pm

    The answer to the short question is “No”. Warp level branch divergence around a __syncthreads() instruction will cause a deadlock and result in a kernel hang. Your code example is not guaranteed to be safe or correct. The correct way to implement the code would be like this:

    __global__ void kernel(...)
    
        if (tidx < N) {
            // Code stanza #1
        }
    
        __syncthreads();
    
    
        if (tidx < N) {
            // Code stanza #2
        }
    
        // etc
    }
    

    so that the __syncthreads() instructions are executed unconditionally.


    EDIT: Just to add a bit of additional information which confirms this assertion, __syncthreads() calls get compiled into the PTX bar.sync instruction on all architectures. The PTX2.0 guide (p133) documents bar.sync and includes the following warning:

    Barriers are executed on a per-warp basis as if all the threads in a
    warp are active. Thus, if any thread in a warp executes a bar
    instruction, it is as if all the threads in the warp have executed the
    bar instruction. All threads in the warp are stalled until the barrier
    completes, and the arrival count for the barrier is incremented by the
    warp size (not the number of active threads in the warp). In
    conditionally executed code, a bar instruction should only be used if
    it is known that all threads evaluate the condition identically (the
    warp does not diverge). Since barriers are executed on a per-warp
    basis, the optional thread count must be a multiple of the warp size.

    So despite any assertions to the contrary, it is not safe to have conditional branching around a __syncthreads() call unless you can be 100% certain that every thread in any given warp follows the same code path and no warp divergence can occur.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it safe to use the same Encoding object from different threads? By using
MSDN states that gacutil.exe is not safe to use for production servers . Is
PHP's documentation page for flock() indicates that it's not safe to use under IIS.
Is it safe to use the using statement on a (potentially) null object? Consider
Is the June 2009 WPF Toolkit safe to use in applications that will be
Though internal, I presumed that it is safe to use ConcurrentIdentityWeakKeyHashMap generally. However, the
We use Source Safe and are migrating to Source Gear. We know that we
Building a commercial product may use various open source libraries that have use of
Possible Duplicate: Using GetHashCode for getting Enum int value Is it safe to use
I have unescaped data from users. So is it safe to use like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.