how can i make a new thread and synchronicity?

i did these code for a new thread and i use a synchronization, but the input values can be adressed to another variable :(
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
struct arg_readmultithread
    {
        consolewindow *consolewindowpointer=NULL;
        string *strreaded=NULL;
        char *chrreaded=NULL;
        double *dblreaded=NULL;
    };
 
    arg_readmultithread arg_multiread;
 
    void APIDoEvents()
    {
        MSG msg;
        BOOL result;
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
 
    HANDLE ghWriteEvent{CreateEvent(
        NULL,               // default security attributes
        TRUE,               // manual-reset event
        FALSE,              // initial state is nonsignaled
        TEXT("WriteEvent")  // object name
        )};
 
 
    void multi_read(char *chr, string *str, double *dbl)
    {
        if(blnread==true)
        {
             while(blnread==true)
            {
                APIDoEvents();
                //WaitForSingleObject( hIOMutex, INFINITE );
            }
        }
        SetEvent(ghWriteEvent);
        blnread=true;
 
        //WaitForSingleObject( hIOMutex, INFINITE );
        strreaded=str;
        //dblreaded=dbl;
        ostringstream  address;
        address << strreaded;
        std:string name = address.str();
        static int i=0;
        i=i+1;
        DebugText("on function" + to_string(i) + ": "+ name);
        WaitForSingleObject(ghWriteEvent, INFINITE);
    }
 
    static void *multithreadproc(void *pThisArg)
    {
        arg_readmultithread *pThis = static_cast<arg_readmultithread*>(pThisArg);
 
        pThis->consolewindowpointer->multi_read(NULL,pThis->strreaded,pThis->dblreaded);
        return nullptr;//terminates the thread
    }
 
    pthread_t some_thread;
 
    void read(string &txttext)
    {
        delay(100);
        arg_multiread.consolewindowpointer=this;
        arg_multiread.strreaded=&txttext;
        pthread_create(&some_thread, nullptr, consolewindow::multithreadproc,static_cast<void*>(&arg_multiread));
    }


see my debug output:
before read: 0x28fb00
before read2: 0x28faf8
before read3: 0x28faf4//no match
on function1: 0x28fb00
on function2: 0x28faf4 //no match
on function3: 0x28faf8

the 'function3' isn't igual to 'read3' :(
the variable adress isn't correct :(
the values are added to the variables, but with incorrect adress's. i have the results on wrong order.
why?
Last edited on
Where is this 'before read' output?

What sense does it make to start threads when you want it sequential?

Where do you set blnread = false?

What exactly are you trying to accomplish?
Maybe APIDoEvents() in second thread spend more time that in third. So third thread write DebugText() earlier, and write there that it's second thread.
i did several errors:
1 - in these case, i can't use event functions, but mutex code;
2 - i only can close(or wait) the thread until the key return is pressed.
so see the actual code:
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
struct arg_readmultithread
    {
        consolewindow *consolewindowpointer=NULL;
        string *strreaded=NULL;
        char *chrreaded=NULL;
        double *dblreaded=NULL;
        string txtwrite="";
    };

    arg_readmultithread arg_multiread;
    HANDLE    hIOMutex;

    void APIDoEvents()
    {
        MSG msg;
        BOOL result;
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE ))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    pthread_t some_thread;
    pthread_mutex_t myMutex;

    void multi_read(char *chr, string *str, double *dbl)
    {
        pthread_mutex_lock(&myMutex);//activate the mutex, for synchronization of the new thread
        while(blnread==true)
        {
            APIDoEvents();
        }

        blnread=true;

        strreaded=str;
        dblreaded=dbl;
        ostringstream  address;
        address << strreaded;
        std:string name = address.str();
        static int i=0;
        i=i+1;
        DebugText("on function" + to_string(i) + ": "+ name);
    }

    static void *multithreadproc(void *pThisArg)
    {
        arg_readmultithread *pThis = static_cast<arg_readmultithread*>(pThisArg);//getting the structure values
        pThis->consolewindowpointer->multi_read(NULL,pThis->strreaded,pThis->dblreaded);
        return nullptr;//terminates the thread
    }



    void read(string &txttext)
    {
        arg_multiread.consolewindowpointer=this;
        arg_multiread.strreaded=&txttext;
        pthread_create(&some_thread, nullptr, &consolewindow::multithreadproc,static_cast<void*>(&arg_multiread));
    }

on richedit window procedure:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
case WM_KEYUP:
            {
                if((wParam==VK_RETURN) && (richedit->blnread==true))
                {
                    //pthread_mutex_unlock(&richedit->myMutex); //i moved down for don't lose the data before add it to variable
                    richedit->blnread=false;
                    if((is_number(richedit->readstring)==true))
                        if(richedit->dblreaded!=NULL)
                            *richedit->dblreaded=stod(richedit->readstring);
                    else
                    {
                        *richedit->dblreaded=0;

                    }
                    if(richedit->strreaded!=NULL)
                        *richedit->strreaded=richedit->readstring;
                    richedit->readstring="";
pthread_mutex_unlock(&richedit->myMutex);//now i can release the new thread.. because the return key was up.

                }

when the richedit is created:
pthread_mutex_init(&myMutex,NULL);
and now i use it:
1
2
3
4
5
6
7
8
9
10
11
12
13
string intnumber;
    ostringstream  address;
    address << &intnumber;
    std:string name = address.str();
    DebugText("before read: " + name);
    cw.read(intnumber);
string intnumber2;
    address.str("");
    address.clear();
    address << &intnumber2;
    name = address.str();
    DebugText("before read2: " + name);
    cw.read(intnumber2);

i must do that for get the variable adress, for i debug the code.
the DebugText() it's my own debug window for test the variables values ;)
i accept sugestions from code ;)
but seems great
Last edited on
Topic archived. No new replies allowed.