VEXcode IQ C++ Unofficial documentation for version 3.0.4.1
Loading...
Searching...
No Matches
vex_thread.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------*/
2/* */
3/* Copyright (c) Robomatter 2016, All rights reserved. */
4/* */
5/* Module: vex_thread.h */
6/* Author: James Pearman */
7/* Created: 8 Nov 2017 */
8/* */
9/* Revisions: */
10/* V1.00 TBD - Initial release */
11/* */
12/*----------------------------------------------------------------------------*/
13
14#ifndef VEX_THREAD_CLASS_H
15#define VEX_THREAD_CLASS_H
16
17#include <chrono>
18
19/*-----------------------------------------------------------------------------*/
22/*---------------------------------------------------------------------------*/
23
24namespace vex {
28 class thread {
29 private:
30 static int _labelId;
31 int (* _callback)(void);
32
33 public:
34 thread() : _callback( NULL ) {};
39 thread( int (* callback)(void) );
44 thread( void (* callback)(void) ) : thread( (int (*)(void)) callback ) {}
45 ~thread();
46
47 static const int32_t threadPrioritylow = 1;
48 static const int32_t threadPriorityNormal = 7;
49 static const int32_t threadPriorityHigh = 15;
50
55 int32_t get_id();
56
60 void join();
61
65 void detach() {};
66
71 bool joinable();
72
77
82 void swap( thread& __t );
83
84 // non standard
88 void interrupt();
89
94 void setPriority( int32_t priority );
95
100 int32_t priority();
101
102
107 static int32_t hardware_concurrency();
108
114 static void swap( thread& __x, thread& __y ) { __x.swap(__y); }
115 };
116
117 namespace this_thread {
118
123 int32_t get_id();
124
128 void yield();
129
131
135 void sleep_for( uint32_t time );
136
141 template<typename _Rep, typename _Period>
142 inline void
143 sleep_for(const std::chrono::duration<_Rep, _Period>& __rtime) {
144 if (__rtime <= __rtime.zero())
145 return;
146
147 int __ms = std::chrono::duration_cast<std::chrono::milliseconds>(__rtime).count();
148 sleep_for( (uint32_t)__ms );
149 }
150
152
156 void sleep_until( uint32_t time );
161 template<typename _Clock, typename _Duration>
162 inline void
163 sleep_until(const std::chrono::time_point<_Clock, _Duration>& __atime)
164 {
165 auto __now = _Clock::now();
166 if (__now < __atime)
167 sleep_for(__atime - __now);
168 }
169
170 // non standard
175 void setPriority( int32_t priority );
180 int32_t priority();
181 };
182
186 class mutex {
187 private:
188 uint32_t _sem;
189
190 public:
191 mutex();
192 ~mutex();
193
197 void lock();
198
203 bool try_lock();
204
208 void unlock();
209 };
210};
211
212
213#endif // VEX_THREAD_CLASS_H
Use this class to create and control mutexes.
Definition vex_thread.h:186
void lock()
Locks the mutex and blocks if the mutex is not available.
void unlock()
Unlocks the mutex.
bool try_lock()
Try to lock the mutex and returns if the mutex is not available.
Use this class to create and control threads.
Definition vex_thread.h:28
void setPriority(int32_t priority)
Sets the priority of the thread.
void * native_handle()
Gets the pointer to the native handle of the thread.
thread(void(*callback)(void))
Creates a thread object.
Definition vex_thread.h:44
void detach()
Permits the thread to execute from the thread handle.
Definition vex_thread.h:65
void join()
Waits for the other thread to finish its execution.
int32_t get_id()
Gets the ID of the thread.
thread(int(*callback)(void))
Creates a thread object.
int32_t priority()
Gets the priority of the thread.
void interrupt()
Stops the thread.
static int32_t hardware_concurrency()
Gets the number of concurrent threads supported by the hardware.
void swap(thread &__t)
Swaps the thread IDs with another specified thread in the parameter.
bool joinable()
Checks whether the thread is joinable.
static void swap(thread &__x, thread &__y)
Swaps two threads specified in the parameters.
Definition vex_thread.h:114
Bumper switch device class
Definition vex_brain.h:24
void setPriority(int32_t priority)
Sets the priority of the current thread.
void sleep_for(uint32_t time)
sleep_for
void sleep_until(uint32_t time)
sleep_until
int32_t get_id()
Gets the ID of the thread.
void yield()
Suspends the current thread.
int32_t priority()
Gets the priority of the current thread.