Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Detecting changes in a C++ Thread

Status
Not open for further replies.

Klen

Member level 1
Joined
Nov 15, 2010
Messages
36
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Location
Germany
Activity points
1,602
Sorry for the vague title, but I couldn`t think of anything! O:)

Well, my problem is as follows. I have a C++ project (regarding a Motion Simulator Unit). I wrote a thread which generates a PPS(pulse per second) from a FTDI Module. The moment I detect a rising edge of my PPS (and then succesively after every 20ms till the start of a new rising edge), I need to inform my main code of the detection and change a value therein. I have been looking at some windows functions like CreatEvent etc, but at the end of the day, it all leaves me more confused! Could anyone please lenmd me some badly needed help!

I will try rephrasing the question in a general way: If in a thread, a variable changes a value, how does one notify the main program (in a diferent .cpp file ofcourse) about the change? Would extern work or I have to use some event signalling? Any nudge in the right direction would be very much appreciated!

Regards
Klen
 

here is a example using threads and events
Code:
#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;

HANDLE event;           // event object
int eventNumber=0;      // data to pass between threads

// the thread function
DWORD WINAPI ThreadProc(void* x )
{
        cout << "thread started" << endl;
        for(int i=0; i<10; i++)
        {
            eventNumber++;
            SetEvent(event);    // ser event
            Sleep(1000);        // sleep for 1 second
        }
        return 0;
}

// test from to call the thread function and wait for event
int main(int argc, char *argv[])
{
    // creat event handle and the thread
    event = CreateEvent(0,true,false,0);
    CreateThread( NULL, 0, ThreadProc, NULL, 0, NULL); // create thread
    while(eventNumber < 10)
    {
        // loop waiting for the event, reset and display data
        WaitForSingleObject(event, INFINITE);
        ResetEvent(event);
        cout << "event number" << eventNumber << endl;
    }
    return 0;
}
when run it prints
Code:
thread started
event number1
event number2
event number3
event number4
event number5
event number6
event number7
event number8
event number9
event number10
this is very simple - in more complex situations you need to think about synchronisation and what is accessing objects when etc. see
Synchronization Object Security and Access Rights (Windows)
 
  • Like
Reactions: Klen

    Klen

    Points: 2
    Helpful Answer Positive Rating
horace1, you are truly a life saver. Thank you very much for the code which explains the logic so much more easily as compared to the incomplete examples available online.

If I may, one thing has been bothering me for a long time. In your example , as the thread and the calling function is in one file, you were able to declare 'eventNumber' as a global variable. Do I have to declare it twice when my thread and calling functions are in different codes or it's better to declare it as a global variable in a common header file?

But, once again, I need to thank you for clearing such an important concept for me in such few lines. I truly appreciate it!
 

you define the global variables in one file aand declare then external in other files, see post
https://www.edaboard.com/threads/206953/#post873249

in this case main.cpp could be
Code:
// main program

#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;

#include "ThreadProc.h"

// test from to call the thread function and wait for event
int main(int argc, char *argv[])
{
    // creat event handle and the thread
    event = CreateEvent(0,true,false,0);
    CreateThread( NULL, 0, ThreadProc, NULL, 0, NULL); // create thread
    while(eventNumber < 10)
    {
        // loop waiting for the event, reset and display data
        WaitForSingleObject(event, INFINITE);
        ResetEvent(event);
        cout << "event number" << eventNumber << endl;
    }
    return 0;
}
header file threadproc.h could be
Code:
// ThreadProc.h - declare variables and functions

extern HANDLE event;           // event object
extern int eventNumber;        // data to pass between threads

// the thread function
DWORD WINAPI ThreadProc(void* x );
and implementation file treadproc.c could be
Code:
// ThreadProc.c - define variables and functions

#include <iostream>
#include <windows.h>
#include <process.h>
using namespace std;
#include "ThreadProc.h"

HANDLE event;           // event object
int eventNumber=0;      // data to pass between threads

// the thread function
DWORD WINAPI ThreadProc(void* x )
{
        cout << "thread started" << endl;
        for(int i=0; i<10; i++)
        {
            eventNumber++;
            SetEvent(event);    // ser event
            Sleep(1000);        // sleep for 1 second
        }
        return 0;
}
 
  • Like
Reactions: Klen

    Klen

    Points: 2
    Helpful Answer Positive Rating
Once again, my humblest gratitude. The concept of events and threads has never been clearer to me before!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top