File: bufr_pthreads.c

package info (click to toggle)
eccodes 2.44.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 150,248 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 285; xml: 183; awk: 66
file content (90 lines) | stat: -rw-r--r-- 2,697 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * (C) Copyright 2005- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
 * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>

#include "eccodes.h"
#define NUM_THREADS 3

/*static int DBL_EQUAL(double d1, double d2, double tolerance)
{
    return fabs(d1-d2) <= tolerance;
}*/

static void* process_bufr(void* arg)
{
    FILE* fin       = (FILE*)arg;
    int err         = 0;
    codes_handle* h = NULL;
    long numSubsets = 0, lVal = 0;
    size_t size = 0, i = 0;
    double* dValues = NULL;
    /* Each thread gets a different message handle */
    h = codes_handle_new_from_file(NULL, fin, PRODUCT_BUFR, &err);
    assert(h);

    /* Check expected values for this BUFR file */
    CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numSubsets), 0);
    assert(numSubsets == 1);
    CODES_CHECK(codes_get_long(h, "rectimeSecond", &lVal), 0);
    assert(lVal == 27);

    CODES_CHECK(codes_set_long(h, "unpack", 1), 0);

    dValues = (double*)malloc(numSubsets * sizeof(double));
    assert(dValues);
    size = numSubsets;
    CODES_CHECK(codes_get_double_array(h, "latitude", dValues, &size), 0);
    for (i = 0; i < size; ++i) {
        /* Specific test for latitudes in this BUFR file */
        assert(dValues[0] < 79 && dValues[0] > 70);
    }
    free(dValues);

    /* Some encoding too */
    CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", 88), 0);
    CODES_CHECK(codes_set_long(h, "blockNumber", 2), 0);
    CODES_CHECK(codes_set_long(h, "#3#verticalSignificanceSurfaceObservations", 8), 0);
    CODES_CHECK(codes_set_long(h, "pack", 1), 0);

    codes_handle_delete(h);
    pthread_exit(NULL);
}

int main(int argc, char** argv)
{
    pthread_t thread1, thread2, thread3;
    int err          = 0;
    FILE* fin        = 0;
    codes_handle* h1 = 0;
    codes_handle* h2 = 0;
    fin              = fopen("../../data/bufr/syno_multi.bufr", "rb");
    assert(fin);

    err = pthread_create(&thread1, NULL, process_bufr, (void*)fin);
    if (err) return 1;

    err = pthread_create(&thread2, NULL, process_bufr, (void*)fin);
    if (err) return 1;

    err = pthread_create(&thread3, NULL, process_bufr, (void*)fin);
    if (err) return 1;

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_join(thread3, NULL);

    fclose(fin);
    codes_handle_delete(h1);
    codes_handle_delete(h2);
    return 0;
}