亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪(fǎng)問(wèn)板塊 發(fā)新帖
查看: 2215 | 回復(fù): 2
打印 上一主題 下一主題

JavaNative 使用C多線(xiàn)程庫(kù)的問(wèn)題 [復(fù)制鏈接]

論壇徽章:
19
CU大;照
日期:2013-03-13 15:32:35CU大;照
日期:2013-09-18 15:15:15CU大;照
日期:2013-05-20 10:46:44CU大;照
日期:2013-05-20 10:46:38CU大;照
日期:2013-05-20 10:46:31CU大;照
日期:2013-05-20 10:46:25CU大牛徽章
日期:2013-05-20 10:46:18CU大;照
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大;照
日期:2013-04-17 11:19:32CU大;照
日期:2013-04-17 11:19:28
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-07-19 11:37 |只看該作者 |倒序?yàn)g覽
Java類(lèi)文件
  1. package com.fangzhaoguo.primefinder;

  2. public class NativeConcurrentPrimeFinder {

  3.         static {
  4.                 System.loadLibrary("libJavaNative_ConcurrentPrimeFinder");
  5.         }

  6.         public NativeConcurrentPrimeFinder() {
  7.         }

  8.         public native boolean isPrime(final long number);

  9.         public native int countPrime(final long number);

  10.         public native int countPrime(final long lower, final long upper);
  11. }
復(fù)制代碼
javah后的C頭文件,并且經(jīng)過(guò)我的修改,加入了自定義的變量、數(shù)據(jù)結(jié)構(gòu)以及函數(shù)
  1. /* DO NOT EDIT THIS FILE - it is machine generated */
  2. #include "jni.h"
  3. #include <pthread.h>
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <math.h>
  7. /* Header for class com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder */
  8. jint total = 0;

  9. typedef struct {
  10.         JNIEnv *env;
  11.         jobject obj;
  12.         jlong lower;
  13.         jlong upper;
  14. } Message;

  15. #ifndef _Included_com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder
  16. #define _Included_com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.         /*
  21.          * Class:     com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder
  22.          * Method:    isPrime
  23.          * Signature: (J)Z
  24.          */
  25.         JNIEXPORT jboolean JNICALL Java_com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder_isPrime(
  26.                         JNIEnv *, jobject, jlong);

  27.         /*
  28.          * Class:     com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder
  29.          * Method:    countPrime
  30.          * Signature: (J)I
  31.          */
  32.         JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder_countPrime__J(
  33.                         JNIEnv *, jobject, jlong);

  34.         /*
  35.          * Class:     com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder
  36.          * Method:    countPrime
  37.          * Signature: (JJ)I
  38.          */
  39.         JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder_countPrime__JJ(
  40.                         JNIEnv *, jobject, jlong, jlong);

  41.         void *countPrimeInRange(void* mess);

  42. #ifdef __cplusplus
  43. }
  44. #endif
  45. #endif
復(fù)制代碼
Mingw編譯的C文件 ,使用了pthread頭文件,并且在鏈接時(shí)使用了pthread,編譯正常
線(xiàn)程調(diào)用時(shí)通過(guò)message來(lái)傳遞JavaNative特有的參數(shù)
  1. /*
  2. * com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder.c
  3. *
  4. *  Created on: 2013年7月18日
  5. *      Author: root
  6. */

  7. #include "com_fangzhaoguo_primefinder_NativeConcurrentPrimeFinder.h"

  8. JNIEXPORT jboolean JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(
  9.                 JNIEnv *env, jobject obj, jlong number) {
  10.         jlong i = 0;
  11.         if (1 >= number) {
  12.                 return 0;
  13.         }
  14.         for (i = 2; i <= sqrt(number); i++) {
  15.                 if (0 == number % i) {
  16.                         return 0;
  17.                 }
  18.         }
  19.         return 1;
  20. }

  21. void *countPrimeInRange(void* mess) {
  22.         jlong i = 0;
  23.         Message* message = (Message*) mess;
  24.         for (i = message->lower; i < message->upper; i++) {
  25.                 if (Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(
  26.                                 message->env, message->obj, i)) {
  27.                         total++;
  28.                 }
  29.         }
  30.         return NULL;
  31. }

  32. JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__J(
  33.                 JNIEnv *env, jobject obj, jlong number) {
  34.         pthread_t thread[4];
  35.         Message* message = (Message*) malloc(sizeof(Message));
  36.         message->env = env;
  37.         message->obj = obj;
  38.         jlong start = 0, end = 0;
  39.         jlong range = number / 4;
  40.         jint i = 0;
  41.         for (i = 0; i < 4; i++) {
  42.                 start = 0 + range * i;
  43.                 if (i < 3) {
  44.                         end = start + range;
  45.                 } else {
  46.                         end = number;
  47.                 }
  48.                 message->lower = start;
  49.                 message->upper = end;
  50.                 pthread_create(&thread[i], NULL, countPrimeInRange, (void*) message);
  51.         }
  52.         return total;
  53. }

  54. JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__JJ(
  55.                 JNIEnv *env, jobject obj, jlong lower, jlong upper) {
  56.         jlong i = 0;
  57.         for (i = lower; i < upper; i++) {
  58.                 if (Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(env, obj,
  59.                                 i)) {
  60.                         total++;
  61.                 }
  62.         }
  63.         return total;
  64. }
復(fù)制代碼
但是現(xiàn)在的問(wèn)題是在java中運(yùn)行時(shí)報(bào)錯(cuò),不管使用C寫(xiě)還是加載boost類(lèi)的C++寫(xiě),都報(bào)相同的錯(cuò)誤。有不使用C/C++多線(xiàn)程的例子,可以正常使用
以下是報(bào)錯(cuò)內(nèi)容
  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: com.fangzhaoguo.primefinder.NativeConcurrentPrimeFinder.countPrime(J)I
  2.         at com.fangzhaoguo.primefinder.NativeConcurrentPrimeFinder.countPrime(Native Method)
  3.         at com.fangzhaoguo.primefinder.Main.timeAndCompute(Main.java:58)
  4.         at com.fangzhaoguo.primefinder.Main.main(Main.java:6)
復(fù)制代碼

論壇徽章:
4
雙子座
日期:2014-08-28 10:08:002015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亞洲杯之阿聯(lián)酋
日期:2015-03-13 03:25:15
2 [報(bào)告]
發(fā)表于 2013-07-20 23:58 |只看該作者
看看是不是編譯的原因,strings看一下你的函數(shù)名的符號(hào)有沒(méi)有多個(gè)@符號(hào),如果有的話(huà),改一下編譯參數(shù)

論壇徽章:
19
CU大牛徽章
日期:2013-03-13 15:32:35CU大;照
日期:2013-09-18 15:15:15CU大;照
日期:2013-05-20 10:46:44CU大牛徽章
日期:2013-05-20 10:46:38CU大;照
日期:2013-05-20 10:46:31CU大;照
日期:2013-05-20 10:46:25CU大;照
日期:2013-05-20 10:46:18CU大牛徽章
日期:2013-04-17 11:19:51CU大牛徽章
日期:2013-04-17 11:19:42CU大牛徽章
日期:2013-04-17 11:19:37CU大;照
日期:2013-04-17 11:19:32CU大;照
日期:2013-04-17 11:19:28
3 [報(bào)告]
發(fā)表于 2013-07-21 11:59 來(lái)自手機(jī) |只看該作者
使用nm看嗎?有下劃線(xiàn)數(shù)量的變化,沒(méi)有發(fā)現(xiàn)@
不過(guò)boost版的問(wèn)題多些
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP