apriltag icon indicating copy to clipboard operation
apriltag copied to clipboard

Dissable threads at compile time with NOTHREADS option

Open Chris-F5 opened this issue 4 months ago • 5 comments

Pull request for #355. My initial idea was to define empty pthread_* functions in pthreads_cross.{c,h} so that when other code uses them the symbols would get resolved. Then I would enforce worker_pool->nthreads==1 so that these vacuous pthread_* functions didnt get used. Problem is that these functions need pthread types in their signatures and we cant just typedef our own because its hard to predict if the standard libs will already define them. For example, I'm trying to cross compile to an embedded arm device for which #import <time.h> imports sys/types.h which imports sys/pthread_types.h which defines pthread types even though pthread.h does not declare pthread_* functions!

So the way it works now is if the user defines NOTHREADS then all #include <pthread.h> and all uses of pthread types or functions are dissabled using #ifdefs. Then in workerpool.c we just ignore the nthreads argument to workerpool_create.

Also workerpool.c:workerpool_get_nprocs and common/pthreads_cross.c:pcthread_get_num_procs were re-implementing the same function. So I made workerpool_get_nprocs just call pcthread_get_num_procs. If NOTHREADS is defined then pcthread_get_num_procs returns 1.

I've tested this quickly by running the following code with and without NOTHREADS.

#include <stdio.h>
#include "apriltag.h"
#include "tag25h9.h"
int main(int argc, char *argv[])
{
    image_u8_t* im = image_u8_create_from_pnm("noexist.pnm");
    apriltag_detector_t *td = apriltag_detector_create();
    td->nthreads = 5;
    apriltag_family_t *tf = tag25h9_create();
    apriltag_detector_add_family(td, tf);
    zarray_t *detections = apriltag_detector_detect(td, im);
    for (int i = 0; i < zarray_size(detections); i++) {
        apriltag_detection_t *det;
        zarray_get(detections, i, &det);
        printf("%f %f\n", det->c[0], det->c[1]);
    }
    apriltag_detections_destroy(detections);
    tag25h9_destroy(tf);
    apriltag_detector_destroy(td);
}

Note that these changes have been made on top of my Remove CRLF line ends commit. So diffing the branch makes it look like all the lines in pthreads_cross.{c,h} have changed even though only some have.

Chris-F5 avatar Oct 01 '24 17:10 Chris-F5