Why are you calling pthread_exit(0) on the main thread, this will terminate
your application immediately.

You can't block on the main UI thread (which calls your JNI callback).
You'll need to find a different synchonization primitive (see AsyncHandlers
in the SDK doc)

On Wed, Aug 24, 2011 at 5:41 PM, arif <arifo...@gmail.com> wrote:

> Hi All,
>
> I ported one muti-threaded native code (binary) to android platform,
> and is working fine,
>
> Now i am trying to run this code by creating apk, created my native
> code as shared library using NDK and
> i am calling this shared library by using JNI, below is the sample
> code,
>
> But it is not working, i am new to java and jni, i did some web
> search,  and i found that i need to attache/detach threads in jni,
>
> It will be very helpful if someone help me to handle this native
> threads in jni.
>
>
> Thanks
>
>
>
>
> /************************** apk code
> ***************************************/
> public class HelloJni extends Activity
> {
>    /** Called when the activity is first created. */
>    @Override
>    public void onCreate(Bundle savedInstanceState)
>    {
>        super.onCreate(savedInstanceState);
>
>        TextView  tv = new TextView(this);
>        tv.setText( stringFromJNI() );
>        setContentView(tv);
>    }
>    public native String  stringFromJNI();
>
>    static {
>        System.loadLibrary("my_lib");
>    }
> }
>
> /****************************** JNI function
> ************************************/
> jstring
> Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject
> thiz)
> {
>                        int rc = native_main();
>                        if (rc == 0)
>                             return (*env)->NewStringUTF(env,
> "Successful!");
>                        else
>                            return (*env)->NewStringUTF(env, "Failure!");
> }
>
> /********************************* Native code
> ***********************************/
> int
> native_main()
> {
>        int cnt;
>        pthread_t thread;
>        for (cnt =0; cnt < 5; cnt++)
>        {
>                if ( 0 != pthread_create (&thread1, NULL, (void *)
> &my_function,
> (void *) cnt))
>                {
>                        return -1;
>                }
>        }
>        pthread_exit(0); /* exit */
>        return 0;
> }
>
> void my_function(void* cnt)
> {
>        int c = (int) cnt;
>
>        switch (c)
>        {
>                case 0:
>                                /* calling native funtion1 */
>                        break;
>                case 1:
>                                /* calling native funtion2 */
>                        break;
>                case 2:
>                                /* calling native funtion3 */
>                        break;
>                case 3:
>                                /* calling native funtion4 */
>                        break;
>                case 4:
>                                /* calling native funtion5 */
>                        break;
>        }
> }
>
> /******************************************************************/
>
> --
> unsubscribe: android-porting+unsubscr...@googlegroups.com
> website: http://groups.google.com/group/android-porting
>

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: http://groups.google.com/group/android-porting

Reply via email to