Skip to content

Commit

Permalink
Fix thread safety problem in Predict (flag_predict_probability) #38
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaldvogel committed Apr 14, 2021
1 parent 330e8a7 commit fef9120
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/main/java/de/bwaldvogel/liblinear/Predict.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@

public class Predict {

private static boolean flag_predict_probability = false;

private static final Pattern COLON = Pattern.compile(":");

/**
* <p><b>Note: The streams are NOT closed</b></p>
*/
static void doPredict(BufferedReader reader, Writer writer, Model model) throws IOException {
static void doPredict(BufferedReader reader, Writer writer, Model model, boolean flag_predict_probability) throws IOException {
int correct = 0;
int total = 0;
double error = 0;
Expand Down Expand Up @@ -146,6 +144,8 @@ private static void exit_with_help() {
}

public static void main(String[] argv) throws IOException {
// Note: This flag is _static_ in predict.c but it causes a thread-safety issue as reported in https://github.com/bwaldvogel/liblinear-java/issues/38
boolean flag_predict_probability = false;
int i;

// parse options
Expand Down Expand Up @@ -182,7 +182,7 @@ public static void main(String[] argv) throws IOException {
FileOutputStream out = new FileOutputStream(argv[i + 2]);
Writer writer = new BufferedWriter(new OutputStreamWriter(out, Linear.FILE_CHARSET))) {
Model model = Linear.loadModel(Paths.get(argv[i + 1]));
doPredict(reader, writer, model);
doPredict(reader, writer, model, flag_predict_probability);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/de/bwaldvogel/liblinear/PredictTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void tearDown() {
private void testWithLines(StringBuilder sb) throws Exception {
try (StringReader stringReader = new StringReader(sb.toString());
BufferedReader reader = new BufferedReader(stringReader)) {
Predict.doPredict(reader, writer, testModel);
Predict.doPredict(reader, writer, testModel, false);
}
}

Expand Down

0 comments on commit fef9120

Please sign in to comment.