Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpThread Class Reference

#include <visp3/core/vpThread.h>

Public Types

typedef void * Args
 
typedef void * Return
 
typedef void *(* Fn )(Args)
 
typedef pthread_t Handle
 

Public Member Functions

 vpThread ()
 
 vpThread (vpThread::Fn fn, vpThread::Args args=NULL)
 
void create (vpThread::Fn fn, vpThread::Args args=NULL)
 
virtual ~vpThread ()
 
void join ()
 
Handle getHandle ()
 
bool joinable ()
 

Protected Attributes

Handle m_handle
 
bool m_isCreated
 
bool m_isJoinable
 

Detailed Description

Class to represent individual threads of execution. This class implements native pthread functionalities if available, or native Windows threading capabilities if pthread is not available under Windows.

There are two examples implemented in testMutex.cpp and testThread.cpp to show how to use this class. The content of test-thread.cpp that hightlights the main functionalities of this class is given hereafter:

#include <iostream>
#include <visp3/core/vpThread.h>
#include <visp3/core/vpTime.h>
vpThread::Return myFooFunction(vpThread::Args args)
{
(void)(args); // Avoid warning: unused parameter args
// do stuff...
return 0;
}
vpThread::Return myBarFunction(vpThread::Args args)
{
(void)(args); // Avoid warning: unused parameter args
// do stuff...
return 0;
}
vpThread::Return myQuxFunction(vpThread::Args args)
{
unsigned int args_ = *((unsigned int *) args);
std::cout << "qux arg: " << args_ << std::endl;
// do stuff...
return 0;
}
int main()
{
unsigned int qux_arg = 12;
vpThread foo;
vpThread bar((vpThread::Fn)myBarFunction);
vpThread qux((vpThread::Fn)myQuxFunction, (vpThread::Args)&qux_arg); // Pass qux_arg to myQuxFunction() function
vpTime::wait(1000); // Sleep 1s to ensure myQuxFunction() internal printings
std::cout << "Joinable after construction:" << std::endl;
std::cout << "foo: " << foo.joinable() << std::endl;
std::cout << "bar: " << bar.joinable() << std::endl;
std::cout << "qux: " << qux.joinable() << std::endl;
foo.create((vpThread::Fn)myFooFunction);
std::cout << "Joinable after creation:" << std::endl;
std::cout << "foo: " << foo.joinable() << std::endl;
std::cout << "bar: " << bar.joinable() << std::endl;
std::cout << "qux: " << qux.joinable() << std::endl;
if (foo.joinable()) foo.join();
if (bar.joinable()) bar.join();
if (qux.joinable()) qux.join();
std::cout << "Joinable after joining:" << std::endl;
std::cout << "foo: " << foo.joinable() << std::endl;
std::cout << "bar: " << bar.joinable() << std::endl;
std::cout << "qux: " << qux.joinable() << std::endl;
return 0;
}

More examples are provided in Tutorial: How to use multi-threading capabilities.

Examples:
grabRealSense.cpp, testForceTorqueAti.cpp, testMutex.cpp, testRealSense.cpp, testThread.cpp, tutorial-face-detector-live-threaded.cpp, tutorial-grabber-opencv-threaded.cpp, and tutorial-grabber-v4l2-threaded.cpp.

Definition at line 31 of file vpThread.h.

Member Typedef Documentation

typedef void* vpThread::Args

Definition at line 35 of file vpThread.h.

typedef pthread_t vpThread::Handle

Definition at line 38 of file vpThread.h.

typedef void* vpThread::Return

Definition at line 36 of file vpThread.h.

Constructor & Destructor Documentation

vpThread::vpThread ( )
inline

Default constructor that does nothing. To attach a function to this thread of execution you need to call create().

Definition at line 50 of file vpThread.h.

vpThread::vpThread ( vpThread::Fn  fn,
vpThread::Args  args = NULL 
)
inline

Construct a thread object that represents a new joinable thread of execution. The new thread of execution calls fn passing args as arguments.

Parameters
fn: A pointer to a function.
args: Arguments passed to the call to fn (if any).

Definition at line 60 of file vpThread.h.

References create().

virtual vpThread::~vpThread ( )
inlinevirtual

Destroy the thread.

Definition at line 97 of file vpThread.h.

References join(), and m_handle.

Member Function Documentation

void vpThread::create ( vpThread::Fn  fn,
vpThread::Args  args = NULL 
)
inline

Creates a thread object that represents a new joinable thread of execution.

Parameters
fn: A pointer to a function.
args: Arguments passed to the call to fn (if any).
Examples:
testMutex.cpp, and testThread.cpp.

Definition at line 70 of file vpThread.h.

References vpException::cannotUseConstructorError, vpException::fatalError, m_handle, m_isCreated, and m_isJoinable.

Referenced by vpThread().

Handle vpThread::getHandle ( )
inline

Returns a value used to access implementation-specific information associated to the thread.

Definition at line 135 of file vpThread.h.

References m_handle.

void vpThread::join ( )
inline

This function return when the thread execution has completed. This blocks the execution of the thread that calls this function until the function called on construction returns (if it hasn't yet).

After a call to this function, the thread object becomes non-joinable and can be destroyed safely.

See Also
joinable()
Examples:
testThread.cpp.

Definition at line 116 of file vpThread.h.

References m_handle, and m_isJoinable.

Referenced by ~vpThread().

bool vpThread::joinable ( )
inline

Returns whether the thread object is joinable.

A thread object is not joinable in any of these cases:

  • if it was default-constructed and create() was not called.
  • if join() has been called.
See Also
join()
Examples:
testThread.cpp.

Definition at line 149 of file vpThread.h.

References m_isJoinable.

Member Data Documentation

Handle vpThread::m_handle
protected

Thread handle.

Definition at line 155 of file vpThread.h.

Referenced by create(), getHandle(), join(), and ~vpThread().

bool vpThread::m_isCreated
protected

Indicates if the thread is created.

Definition at line 156 of file vpThread.h.

Referenced by create().

bool vpThread::m_isJoinable
protected

Indicates if the thread is joinable.

Definition at line 157 of file vpThread.h.

Referenced by create(), join(), and joinable().