Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / mapping / matlab / testMex / test.c @ 959

History | View | Annotate | Download (618 Bytes)

1
#include <mex.h>
2
#include <unistd.h>
3

    
4
pthread_t counter;
5

    
6
void* count(void* arg);
7
int first_time = 1, global = 0;
8

    
9
void mexFunction(int nlhs, 
10
        mxArray* ret[], int nrhs, const mxArray* args[])
11
{
12
        /*deliberately ignore nrhs.        */
13
        if(nlhs != 1){
14
                mexErrMsgTxt("Error - only returns one number.\n");
15
        }
16
        else{
17
                
18
                if(first_time){
19
                        /*Deploy a thread to count.*/
20
                        pthread_create(&counter, NULL, count, NULL);
21
                        first_time = 0;
22
                }
23

    
24
                ret[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
25
                double* value = mxGetPr(ret[0]);
26
                *value = (double)global;        
27
        }
28
}
29

    
30
void* count(void* arg){
31
        while(1){
32
                global++;
33
                usleep(10000);
34
        }
35
}