Question

I've got a tiny question on treads matching between Java threads created by new Thread() and gotten by Thread.currentThread() and native Linux thread could being created ty pthread_create() function call.

The question is: consider I would like to call some native Linux function with pthread_t parameter in order to get some operation on that thread, then is it possible to create thread in Java ( just like an ordrinary Java Thread ) get its id (by calling Thread.getId()) and pass that id through JNA call to native function ? Is it reasonable to expect quite a right functions behaviour ?

Or there is some other ( right ) way ?

Thank you very much for you time and for your patiance !!!!

Was it helpful?

Solution 3

thank all of you very much!

It seems that one of the easiest way to get such a behavior is to pass ZERO as threadId and expect thread with such a call being affined to a core you wish.

Of course there is another way to affine a thread to a core ( from Java PL ) but you have to decide upon cpu_set_t type substitution as well as its size.

OTHER TIPS

You could use a JNA call to a native function you write and that then queries the current thread. (pthread_self for example).

I don't know if the other way will work or is possible, a quick google didn't show anything but I'm not a JNA expert.

If you need a specific thread you could always write a JNA function that returns the linux thread handle. When your thread is started call that function and record the result for future use. I don't know if there are any guarantees made about a Java thread always corresponding to a given System thread though.

If you're already using JNA, call pthread_self() (from the target thread) to get your pthread_t structure back (it is opaque), then pass that to your function that accepts a pthread_t. You may have to dig around in the *.h include files to figure out how to receive the pthread_t structure back into JNA.

UPDATE: this mapping might help. Sounds like you just need the opaque pthread_t structure, you don't need to dig around inside the structure like that guys is trying to do. But you can use his code as an example of the JNA mapping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top