YARP
Yet Another Robot Platform
os/threads/threads.cpp

Demonstrate the basic use of threads. See also os/periodicthread/periodicthread.cpp

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
/*
* Show thread basic functionalities, you may want to have a look at the
* ratethread example.
*/
#include <yarp/os/Thread.h>
#include <yarp/os/Time.h>
#include <cstdio>
constexpr double thread1_delay = 1.0;
constexpr double thread2_delay = 0.5;
constexpr double main_delay = 3.0;
class Thread1 : public Thread
{
public:
bool threadInit() override
{
printf("Starting thread1\n");
return true;
}
void run() override
{
while (!isStopping()) {
printf("Hello, from thread1\n");
yarp::os::Time::delay(thread1_delay);
}
}
void threadRelease() override
{
printf("Goodbye from thread1\n");
}
};
class Thread2 : public Thread
{
public:
bool threadInit() override
{
printf("Starting thread2\n");
return true;
}
void run() override
{
yarp::os::Time::delay(thread2_delay);
while (!isStopping()) {
printf("Hello from thread2\n");
yarp::os::Time::delay(thread2_delay);
}
}
void threadRelease() override
{
printf("Goodbye from thread2\n");
}
};
int main()
{
Network yarp;
Thread1 t1;
Thread2 t2;
printf("Starting threads...\n");
t1.start();
t2.start();
printf("started\n");
yarp::os::Time::delay(main_delay);
printf("stopping threads...\n");
t1.stop();
t2.stop();
printf("stopped\n");
return 0;
}
Network.h
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
Thread.h
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Time.h
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114