From owner-FreeBSD-tech-jp@jp.freebsd.org  Tue Oct 24 00:00:10 2000
Received: (from daemon@localhost)
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) id AAA21948;
	Tue, 24 Oct 2000 00:00:10 +0900 (JST)
	(envelope-from owner-FreeBSD-tech-jp@jp.FreeBSD.org)
Received: from miro.ht.sfc.keio.ac.jp (miro.ht.sfc.keio.ac.jp [133.27.170.10])
	by castle.jp.freebsd.org (8.9.3+3.2W/8.7.3) with ESMTP id AAA21943
	for <freebsd-tech-jp@jp.freebsd.org>; Tue, 24 Oct 2000 00:00:09 +0900 (JST)
	(envelope-from slash@ht.sfc.keio.ac.jp)
Received: from localhost (dali [133.27.170.2])
	by miro.ht.sfc.keio.ac.jp (8.9.2/3.7W) with ESMTP id AAA21269
	for <freebsd-tech-jp@jp.freebsd.org>; Tue, 24 Oct 2000 00:00:09 +0900 (JST)
To: freebsd-tech-jp@jp.freebsd.org
From: Tadashi OKOSHI <slash@ht.sfc.keio.ac.jp>
X-Mailer: Mew version 1.94 on Emacs 19.34 / Mule 2.3 (SUETSUMUHANA)
Mime-Version: 1.0
Content-Type: Text/Plain; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit
Message-Id: <20001024000009A.slash@ht.sfc.keio.ac.jp>
Date: Tue, 24 Oct 2000 00:00:09 +0900
X-Dispatcher: imput version 990905(IM130)
Lines: 89
Reply-To: FreeBSD-tech-jp@jp.freebsd.org
Precedence: list
X-Distribute: distribute version 2.1 (Alpha) patchlevel 24e+000315
X-Sequence: FreeBSD-tech-jp 2965
Subject: [FreeBSD-tech-jp 2965] pthread in shared library & Java
Errors-To: owner-FreeBSD-tech-jp@jp.freebsd.org
Sender: owner-FreeBSD-tech-jp@jp.freebsd.org
X-Originator: slash@ht.sfc.keio.ac.jp

$B$*$*$3$7$H?=$7$^$9!%(B

FreeBSD4.0R + JDK1.1.8$B>e$G!$%M%$%F%#%V%9%l%C%IFb$G(Bpthread$B$r;H$&(B
Java$B$N%W%m%0%i%_%s%0$r$7$F$$$k$N$G$9$,!$(Bpthread$B$N%*%V%8%'%/%H$N%j%s%/(B
$B$N4X78$G:$$C$F$$$^$9!%(B

$B0J2<$K%W%m%0%i%`$rE:IU$7$^$9$,!$Nc$($P(BJava$B$N%M%$%F%#%V%a%=%C%I$NCf$G(B
pthread_create$B$r:n$j$?$$>l9g$K!$(B

  (1)gcc -pthread -c native.c\
     -I/usr/local/jdk1.1.8/include/\
     -I/usr/local/jdk1.1.8/include/freebsd/
     $B$H(B-pthread$B%*%W%7%g%s$r$D$1$F%3%s%Q%$%k$9$k!%(B

  (2)gcc -shared -o libTestClassNative.so native.o
     cp libTestClassNative.so /usr/local/jdk1.1.8/lib
     $B%7%'%"%I%i%$%V%i%j$r:n@.!%(BJDK$B$N%i%$%V%i%j%G%#%l%/%H%j$K@_CV!%(B

  (3)Java$B$N%/%i%9Cf$G!$(Bstatic{System.loadLibrary("TestClassNative");}
     $B$H$d$C$F!$>e5-$N%i%$%V%i%j$r%m!<%I$5$;$k!%(B

$B$H$7$^$9$,!$(B(3)$B$N(BJava$B%/%i%9$r(B java$B%3%^%s%I$G<B9T$7$h$&$H$9$k$H(B
----------------------------------------------------------------------
/usr/local/jdk1.1.8/lib/libTestClassNative.so: Undefined symbol "pthread_create" (libTestClassNative.so)
----------------------------------------------------------------------
$B$H=P$F$7$^$$$^$9!%(B

$B$I$&$d$C$?$i!$(Bpthread$B$r%j%s%/$5$;$k$3$H$,$G$-$k$N$G$7$g$&$+!)(B
FreeBSD 3.0$B0J9_$N(Bpthread$B$G$O!$(Bgcc -pthread$B$H$$$&8GM-$N%*%W%7%g%s$,(B
$B$G$-$?4XO"$G!$%f!<%6$O(B -lpthread$B$7$J$/$F$h$$$G$9$h$M!)(B
$B5U$K(B/usr/lib, /usr/local/lib$B$J$I$K(Blibpthread.a$B$H$+$,$J$$$N$G!$(B
(2)$B$N;~E@$G!$!V(B-lpthread $B!W$H$+$$$FL@<(E*$K%j%s%/$5$;$k$3$H$,$G$-$J$$$7!%(B

$B$b$7$+$7$?$i2?$+$r8+Mn$H$7$F$$$k$h$&$J5$$,$7$^$9$,!$(B
$B$=$&$G$7$?$i$4$a$s$J$5$$!%(B

$B$h$m$7$/8f4j$$CW$7$^$9!%(B


$BBg1[(B

---------------------------------------------------------------------------
Java$B%=!<%9%3!<%I(B
---------------------------------------------------------------------------
public class TestClass{

  static{
    System.loadLibrary("TestClassNative");
  }

  public TestClass(){
    System.out.println("Class TestClass was instanciated!");
  }

  public native void nativedo();

  public static void main(String[] args){
    TestClass a = new TestClass();
    a.nativedo();
  }

}

---------------------------------------------------------------------------
$B%7%'%"%I%i%$%V%i%j$H$J$k(Bnative method$B$N%3!<%I(B
---------------------------------------------------------------------------
#include "TestClass.h"
#include <pthread.h>

void child_thread(){
  fprintf(stderr, "child thread : start!\n");
  fprintf(stderr, "child thread : finish!\n");
}

JNIEXPORT void JNICALL Java_A_nativedo(JNIEnv *env, jobject thisobject){

  pthread_t th;

  fprintf(stderr, "nativedo: start!\n");

  /*pthread_init();*/
  pthread_create(&th, NULL, (void*)&child_thread, NULL);
  pthread_detach(th);

  fprintf(stderr, "nativedo: finish!\n");
}
---------------------------------------------------------------------------

$BBg1[(B
