Thread

include: co/thread.h.

#Thread

Coost v3.0.1 removed the header file co/thread.h, and removed the global class Thread and Mutex, which are nearly the same as std::thread and std::mutex in C++11. Users can use the std version directly.

#co::thread_id

uint32 thread_id();
  • Returns the id value of the current thread.

#co::sync_event

Coost v3.0.1 removed the global SyncEvent, please use co::sync_event instead.

#constructor

explicit sync_event(bool manual_reset=false, bool signaled=false);
  • Constructor, parameter manual_reset indicates whether to manually reset the event to unsynced state, parameter signaled indicates whether the initial state is synced.

#sync_event::reset

void reset();
  • Reset the event to unsynced state.
  • When manual_reset is true in the constructor, users need manually call this method after calling wait() to set the event to unsynced state.

#sync_event::signal

void signal();
  • Generates a sync signal, set the event to synced state.

#sync_event::wait

1. void wait();
2. bool wait(uint32 ms);
  • 1, wait until the event becomes synced.
  • 2, wait until the event becomes synced or timed out. The parameter ms specifies the timeout in milliseconds. Returns true if the event becomes synced, false otherwise.
  • When manual_reset is false in the constructor, wait() will automatically set the event to unsynced state.

#Code Example

#include "co/co.h"

bool manual_reset = false;
co::sync_event ev(manual_reset);

void f1() {
     if (!ev.wait(1000)) {
         LOG << "f1: timedout..";
     } else {
         LOG << "f1: event signaled..";
         if (manual_reset) ev.reset();
     }
}

void f2() {
     LOG << "f2: send a signal..";
     ev.signal();
}

int main(int argc, char** argv) {
     std::thread(f1).detach();
     std::thread(f2).detach();
     co::sleep(3000);
     return 0;
}

#co::tls

template<typename T>
class tls;

co::tls wraps the system’s thread local interface.

thread_ptr was removed in v3.0.1, and co::tls was provided instead.

#constructor

tls();
  • The constructor, allocate system resources and initialize.

#tls::get

T* get() const;
  • Return the pointer value owned by the current thread.

#tls::set

void set(T* p);
  • Set the pointer value owned by the current thread.

#tls::operator->

T* operator->() const;
  • Returns the pointer value owned by the current thread.

#tls::operator*

T& operator*() const;
  • Returns a reference to the object pointed to by the pointer owned by the current thread.

#operator==

bool operator==(T* p) const;
  • Check whether the pointer owned by the current thread is equal to p.

#operator!=

bool operator!=(T* p) const;
  • Check whether the pointer owned by the current thread is not equal to p.

#operator bool

explicit operator bool() const;
  • Returns true if the pointer owned by the current thread is not NULL, false otherwise.