TLightweightEvent (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Language:

Versions:

Description

This example illustrates the use of the TLightweightEvent class.

The main thread does the following: creates a secondary thread, waits 100 milliseconds, and then sets the application-specific event ReadyFlag. The secondary thread cannot perform a certain operation (display a message) until ReadyFlag is set, so it is blocked for approximately 100 milliseconds.

Code

#include <vcl.h>
#pragma hdrstop
 
#include <tchar.h>
#include <Classes.hpp>
#include <DateUtils.hpp>
#include <iostream>
#include <string>
using namespace std;
// ---------------------------------------------------------------------------
 
#pragma argsused
static TLightweightSemaphore *consoleSemaphore;
 
class TThreadRoadRunner : public TThread {
private:
        TSynchroObject *FReadyFlag;
 
        void __fastcall Execute() {
                FReadyFlag->Acquire(); // Polymorphic call
                cout << "Running started" << endl;
        }
 
public:
        TThreadRoadRunner(bool createSuspended, TSynchroObject *ASyncObj)
                : TThread(createSuspended) {
                FReadyFlag = ASyncObj;
        }
};
 
int _tmain(int argc, _TCHAR* argv[]) {
        TLightweightEvent *readyFlag = new TLightweightEvent();
 
        TThreadRoadRunner *roadRunner = new TThreadRoadRunner(true, readyFlag);
        roadRunner->Start();
 
        Sleep(100);
        readyFlag->SetEvent();
        Sleep(100);
 
        return 0; // Put breakpoint here to see the console output.
}

Uses

Personal tools