Lthread¶
#include <lthread_cpp/lthread.h>
using namespace lthread;
- class Lthread¶
Launches a single lthread in the background.
Member Functions¶
- Lthread()
- Lthread(&Method, params,...)
- Lthread(&Class::Method, params,...)
Creates new lthread object and associates it with an lthread. The constructor copies/moves all arguments args... to an lthread-accessible storage.
- Join(uint64_t timeout_ms)¶
Joins on a single lthread and blocks until the lthread returns.
Parameters timeout_ms(optional, default=0): Milliseconds to wait joining on another lthread. Throws: LthreadTimeout() on timeout.
- void Detach()¶
Marks the lthread launched as detachable to be freed upon return. This is a direct binding to lthread_detach
- lthread_t* Id()¶
Returns: an lthread_t* ptr pointing to the original lthread created by lthread_create
- bool Joinable()¶
Returns: true if the lthread can be joined on(i.e., launched)
Note
Lthread objects are movable but not copyable.
Example¶
#include <vector>
#include <lthread_cpp/lthread.h>
using namespace lthread_cpp;
void MyMethod(std::vector<int> my_vec) {}
void Run()
{
std::vector<int> v{1,2,3,4};
Lthread t1{&MyMethod, v};
t1.Detach();
}
int main()
{
Lthread t{&Run};
t.Detach();
Lthread::Run();
}
cc -std=c++11 test.cc -o test -llthread_cpp -llthread -lpthread -lstdc++ && ./test