0

I have directly tried to convert C++ example of mathGL Into C code here:

#include <mgl2/mgl_cf.h>
#include <mgl2/wnd_cf.h>
#include <mgl2/fltk.h>

#include <pthread.h>
#include <synchapi.h>

//mglFLTK *gr=NULL; // pointer to window
HMGL gr = NULL;//HMGL mgl_create_graph_fltk
void *calc(void *) // function with calculations
{
//  mglPoint pnt; // some data for plot
//  HMDT dat;
//  dat= mgl_create_data () ;
    for(long i=0;;i++) // do calculation
    {
//      long_calculations(); // which can be very long
//      Sleep(200);
        for(uint64_t i=0;i<200000;i++);
//      pnt.Set(2*mgl_rnd()-1,2*mgl_rnd()-1);
        if(gr)
        {
//          gr->Clf(); // make new drawing
            mgl_clf ( gr) ;
            // draw something
//          gr->Line(mglPoint(),pnt,"Ar2");
            mgl_line(gr,0,0,0,2*mgl_rnd()-1,2*mgl_rnd()-1,0,"Ar2",2);

            char str[16]; snprintf(str,15,"i=%ld",i);

//          gr->Puts(mglPoint(),str);
            mgl_puts(gr,0,0,0,str,":C",-.7);
            // don’t forgot to update window
//          gr->Update();
             mgl_wnd_update(gr);

        }
    }
}

But it doesn't work. I've added printf before mgl_wnd_update to printing i in console and it only prints one time: i=0. I'm using windows 10 and installed minGW GCC and mathgl by MSYS2 through eclipse IDE. Though I've created the not threaded one here:

int main(int argc,char **argv)
{
gr = mgl_create_graph_fltk(NULL, "First C graph", NULL, NULL);

mgl_fltk_thr ();

while(1)
{
    Sleep(100);
    mgl_clf ( gr) ;
    // draw something
//          gr->Line(mglPoint(),pnt,"Ar2");
    mgl_line(gr,0,0,0,2*mgl_rnd()-1,2*mgl_rnd()-1,0,"Ar2",-.5);

    char str1[16]; snprintf(str1,15,"i=%ld",1);

//          gr->Puts(mglPoint(),str);
    mgl_puts(gr,0,0,0,str1,":C",-.5);
    // don’t forgot to update window
//          gr->Update();
     mgl_wnd_update(gr);
}
return 0;
}

And this is works. What is wrong with that pthreaded code? It seems that the thread loop only works once. But this loops works well on none-threade program!!

mohammadsdtmnd
  • 330
  • 1
  • 11
  • 1
    Do you have any documentation that states all of the `mgl_*()` functions are multithread-safe? – Andrew Henle Nov 16 '22 at 19:18
  • @AndrewHenle Lets assume they are not safe. but only we have one thread. And this thread continually updates the graph. There is no thing to conflict with, doesn't it? – mohammadsdtmnd Nov 16 '22 at 19:55
  • @AndrewHenle But what I interpret from documentation is that the used functions must be thread compatible. – mohammadsdtmnd Nov 16 '22 at 20:06
  • I can think of at least two things that *could be* wrong with the pthread-using code, but there is not enough information to evaluate those possibilities. We need a [mre]. – John Bollinger Nov 22 '22 at 15:29
  • @JohnBollinger, The pthreaded code is reproducible. I've done this using eclipse, minGW64, MSYS2, windows 10 64bit. Also we need to include it's library other than including headers. – mohammadsdtmnd Nov 23 '22 at 05:44
  • @mohammadsdtmnd, the question is not whether *you* can reproduce the behavior of the pthreaded code, it's whether *we* can. And part of the point of that is to be sure that we have a complete picture. Do follow the link I previously presented if this idea is new to you, or if you're unsure what exactly it means or how to produce one. Producing a MRE a good debugging technique in its own right, and it also helps us help you. Including by identifying contributory issues in the overall program that are not reflected by the excerpt presented in the question. – John Bollinger Nov 23 '22 at 13:04

0 Answers0