首页 > 解决方案 > Android NDK 在线程内不打印

问题描述

我正在使用包含以下功能的渲染器运行 GLSurfaceView:

    public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {
        Engine.NativeMain();
        Engine.logicThread = new Thread(new Runnable() {
            @Override
            public void run()
            {
                while(Engine.isRunning)
                {
                    Engine.NativeUpdate();
                    try{ Thread.sleep(16); }
                    catch(InterruptedException e) { e.printStackTrace();}
                }
            }
        });
        Engine.logicThread.start();
        GLES20.glClearColor(0, 0, 0, 1);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height)
    {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl)
    {
        //By doing that, it is possible to save little battery when the renderer
        //is not dirty
        Engine.NativeRender();
    }

如果我将 Engine.NativeUpdate 移到 onDrawFrame 上,它会打印正常。但是在另一个线程中,它除了打印之外什么都做。我正在使用 __android_log_print 来完成该任务。在我的本机代码中,我保证我没有使用任何线程本地存储。

标签: androidcmultithreadingandroid-ndkshared-libraries

解决方案


我没有记录任何东西,因为我保证我的游戏循环不在线程本地存储中。但我正在设置一个回调函数,它会调用 __android_log_print。该回调是线程本地存储,所以它是空的,因此,没有被调用


推荐阅读