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 8555509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:08:19+00:00 2026-06-11T15:08:19+00:00

Within a C++ JNI function (semi-pseudo included below), there is at least one (and

  • 0

Within a C++ JNI function (semi-pseudo included below), there is at least one (and potentially two) additional thread(s) created at the client_ = new VClient(&callback_) line. I figured that the completion of this function would be enough, but apparently when the next function (another JNICALL function) is “immediately” called after this, it causes a SEGFUALT (“immediately” is in quotes because the function is called as quickly as someone can push the next button). I figure it is because creating the new VClient did not yet complete by the time the Init function returns and the next function is called, since the client_ is being used in the next function.

I am rather new to all this threading business, and I am not sure whether this is a correct line of thinking. I am used to code executing sequentially, thus when the code moves on from the client_ line, it is because everything with that line has been completed. Is it possible for the code to move on from this line, and return from the JNI Init function before the new VClient has been fully created? If so, how would I have this function wait until the class/object has finished being created?

JNIEXPORT void JNICALL Java_com_ClassDir_Init(JNIEnv *env, jobject obj)
{
  LOGI("%s", __PRETTY_FUNCTION__);
  if(!client_)
  {
    LOGI("Initializing client");
    client_ = new VClient(&callback_);
    [Bunch of JNI/JAVA class and methodID lookup and saving]
  }
  else
    LOGI("Client already initialized");
}

*The callback_ is a class that handles sending enum type signals to JNI/JAVA to update on program progress.

  • 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-06-11T15:08:20+00:00Added an answer on June 11, 2026 at 3:08 pm

    You said that VClient constructor creates threads. Creating threads is a synchronous process: the execution of VClient ctor does not continue until the thread is fully created and also most probably started, as i don’t see any other method call on VClient istance to do it. What is not synchronous, is this start of the thread. It does not mean “fully operational”, just that your main thread has instructed the created thread to start running in its own context. When the new thread’s execution loop is set up and entered is completely asynchronous (to your VClient construction) and up to the thread scheduling. So if your “next JNI function” tries to call into that thread and use some resources there, the resource must be already avaliable (which means that you expect the new thread already have progressed to the point of making it available) and the access to that resource must be guarded for threadsafe access.

    So you need to become less new in this threading business 🙂 Look up two essential building blocks:

    1. wait condition (also named “blocking condition variable”). After calling VClient ctor, you will wait on an object which you need to pass into the thread. The thread will notify the condition when it finishes its essential work, hence unblocking the wait in your first thread.
    2. mutex. Whenever there is a resource (data structure) which can be concurrently accessed by execution of multiple threads, you need to lock the mutex before the accessing code and unlock afterwards. The later arriving thread will block on the lock until the first thread finishes. Otherwise when two threads try to modify the same memory, you will get strange results or a straightforward crash.

    These are implemented differently in various APIs per OS and per the framework. Even different frameworks on the same OS tend to be different. But the philosophy is equal.

    Btw, i take for granted that client_ is declared in a way that it does not go out of scope when “the next JNI function” tries to use it. Which probably means a global variable in JNI implementation – i don’t see any class wrapper for the native code.

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

Sidebar

Related Questions

Within a for loop, I'm using the following code to convert from one date
Within Magento orders, there is the option to send the customer a notification to
I used SWIG for generating some native JNI function interface for Irrlicht C/C++ 3D
Within the context of C# on .Net 4.0, are there any built-in objects that
JNI tutorials, for instance this one, cover quite well how to access primitive fields
Within visual Studio is there a way to display the location of a mouse
Within my daily work, I have got to maximize a particular function making use
Within a rake task how does one query the description? Something that would give:
within my login code (index.php) there is a section for facebook login. it checks
Within some library class, is there a way I can I determine which webpage

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.