简体   繁体   中英

Tomcat crashes if make multithreaded native call using Jace JNI

I am using Netbeans to build a java web project (tomcat 6.02 based) which loads a c++ native dll. I am using Jace library which wraps JNI.

In my java code I have a static callback function which I call from c++ code. I am trying to invoke this callback in a new thread using boost.Thread but tomcat just dies without any message or crash report when I do. However if I call the function directly it works fine.

Can you please suggest what might be wrong?

below is my c++ code that causes crash:

//from native method:


for (int i = 0; i < 10; ++i)
{
    MyFunctor func;
    boost::thread t(func);
}

below is my c++ code that works fine:

//from native method:


for (int i = 0; i < 10; ++i)
{
    MyFunctor func;
    func();
}

Functor class (which causes the crash):

class MyFunctor
{
public:

    void operator ()() const
    {
        ArrayList orders, trades;
        //...Fill the above ArrayLists;
        jace::proxy::test::CallBackTest::callbackFunc(orders, trades);
    }
}

following is my java code:

public class CallBackTest {
    public static void callbackFunc(ArrayList arraylist, ArrayList arraylist1) {
        //System.out.println(); the two arraylists;
    }
}

EDIT:

Strangely, following code also works. That is, if I invoke the functor once and then create multiple threads, there is no crash. Also this crash only happens in Tomcat and not if I make a standalone java application. Can anybody please explain why this happens?

MyFunctor func1;
func1();

for (int i = 0; i < 10; ++i)
{
    MyFunctor func;
    boost::thread t(func);
}

In the case that crashes:

You create func inside the for loop. Then you start a thread using func . At the end of your for loop, func and thread will be destroyed. Who knows what'll happen when you fire off a thread and destroy its functor just when the thread wants to access it.

You'll also call back to your java code from another native thread than what you invoked the java code in, which sounds like a very unsafe thing to do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM