当前位置:网站首页>Ncnn thread
Ncnn thread
2022-07-19 03:09:00 【HySmiley】
ncnn-platform.h
Thread and thread local storage class creation - Declaration and Implementation
static unsigned __stdcall start_wrapper(void* args);
// Thread class
class NCNN_EXPORT Thread
{
public:
// Constructors - Initialization list
Thread(void* (*start)(void*), void* args = 0) { _start = start; _args = args; handle = (HANDLE)_beginthreadex(0, 0, start_wrapper, this, 0, 0); }
~Thread() {}
//join Realization - The calling thread waits for the thread to complete , To move on
void join() { WaitForSingleObject(handle, INFINITE); CloseHandle(handle); }
private:
friend unsigned __stdcall start_wrapper(void* args)
{
Thread* t = (Thread*)args;
t->_start(t->_args);
return 0;
}
HANDLE handle;
void* (*_start)(void*);
void* _args;
};
// Thread local storage (TLS)
class NCNN_EXPORT ThreadLocalStorage
{
public:
ThreadLocalStorage() { key = TlsAlloc(); }
~ThreadLocalStorage() { TlsFree(key); }
void set(void* value) { TlsSetValue(key, (LPVOID)value); }
void* get() { return (void*)TlsGetValue(key); }
private:
DWORD key;
};WaitForSingleObject(handle, INFINITE); CloseHandle(handle);
Wait for a kernel object to become notified .
This function needs to pass a kernel object handle , This handle identifies a kernel object , If the kernel object is in an unannounced state , Then this function causes the thread to enter Blocked state ; If the kernel object is in the notified state , Then the function immediately returns WAIT_OBJECT_0. The second parameter indicates the time to wait ( millisecond ), It can deliver INFINITE Indicate to wait indefinitely , If the second parameter is 0, Then the function tests the state of the synchronization object and immediately returns . If the wait times out , This function returns WAIT_TIMEOUT. If the function fails , return WAIT_FAILED.
TLS Reference resources :TLS-- Thread local storage - tungli - Blog Garden
dynamic TLS
Each process in the system has a set of flags in use (in-use flags), Each flag can be set to FREE or INUSE, It means that we should TLS Whether the element is being used .
Usually by calling a group 4 individual API Function to use dynamic TLS:TlsAlloc、TlsSetValue、TlsGetValue and TlsFree.
1) To use dynamic TLS, Must call first TlsAlloc function :
DWORD WINAPI TlsAlloc(void);
2) In order to put a value into the thread PVOID Array , Should be called TlsSetValue function :
BOOL WINAPI TlsSetValue(
__in DWORD dwTlsIndex, // Index value , Represents the specific position in the array
__in_opt LPVOID lpTlsValue // The value to set
);
3) To retrieve a value from the array of threads , The function should be called TlsGetValue:
LPVOID WINAPI TlsGetValue(
__in DWORD dwTlsIndex // Index value
);
4) When you no longer need a reservation TLS Element time , Should be called TlsFree function :
BOOL WINAPI TlsFree(
__in DWORD dwTlsIndex // Index value
);
Mutex Class and ConditionVariable Statement - Thread synchronization Read-write lock / Condition variables,
Mutexes prevent multiple threads from accessing the same shared variable at the same time .
Conditional variables allow one thread to notify other threads of the state changes of a shared variable , And let other threads wait for this notification .
Conditional variables are usually used with mutexes , The reason is that the thread blocks and waits before the condition is not met , Need to access “ Conditions ”, and “ Conditions ” It allows other threads to modify , therefore , visit “ Conditions ” It needs to be locked , Release the lock after access .
class NCNN_EXPORT Mutex
{
public:
Mutex() { InitializeSRWLock(&srwlock); }// Initialize read / write lock
~Mutex() {}
void lock() { AcquireSRWLockExclusive(&srwlock); }// Exclusive access
void unlock() { ReleaseSRWLockExclusive(&srwlock); }// Exclusive release
private:
friend class ConditionVariable;// Friend condition variable class
// NOTE SRWLock is available from windows vista
SRWLOCK srwlock;
};
class NCNN_EXPORT ConditionVariable
{
public:
ConditionVariable() { InitializeConditionVariable(&condvar); }// Initializing condition variables
~ConditionVariable() {}
void wait(Mutex& mutex) { SleepConditionVariableSRW(&condvar, &mutex.srwlock, INFINITE, 0); }// Unlock SRWLock, wait for CV, When the function returns , Automatic locking SRWLock
void broadcast() { WakeAllConditionVariable(&condvar); }// Wake up all threads waiting for condition variables
void signal() { WakeConditionVariable(&condvar); }// Wake up a thread waiting for condition variables
private:
CONDITION_VARIABLE condvar;
};Thread synchronization : Thread synchronization Slim Read-write lock SRWLOCK User mode synchronization object InitializeSRWLock_ Yuwen yingyu's blog -CSDN Blog
Thread condition variables : Thread synchronization Condition variables, CONDITION_VARIABLE User mode synchronization object InitializeConditionVariable_ Yuwen yingyu's blog -CSDN Blog _sleepconditionvariablecs
Thread state : New state 、 The ready state 、 function 、 Blocking 、 Death
Thread synchronization mode :C++ Four ways of thread synchronization (Windows) - Gorgeous Pisces - Blog Garden
边栏推荐
- OpenVINO中的FCOS人脸检测模型代码演示
- 【MySQL】MHA高可用
- 4年开发二面美团最终败给:volatile关键字作用和原理这道面试题
- SysTick定时器的基础学习以及手撕代码
- First knowledge of JPA (ORM idea, basic operation of JPA)
- JDBC connection to MySQL database
- [MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
- 半年时间的思考
- [single chip microcomputer simulation] (XII) instruction system logic operation instruction - logic XOR instruction XRL, accumulator clear 0 and reverse instruction
- Redis' simple dynamic string SDS
猜你喜欢

数据源对象管理(第三方对象资源) & 加载properties文件

All dates in Oracle query time period

MySQL multi table query

About XML file (VI) - the difference between JSON and XML file

Graphql first acquaintance

工具及方法 - Excel插件XLTools

【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码

多项式插值拟合(三)

Advanced usage of the responsibility chain pattern

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?
随机推荐
Obvious things
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
Fiddler抓包
Specifications、多表查询基础
MySQL master-slave replication + read write separation
mysql复制表
3. Asynctool framework principle source code analysis
【PHP】tp6多表连接查询
05_ Service call ribbon
[MCU simulation] (XIII) instruction system logic operation instruction shift instruction
SQL经典练习题(x30)
【单片机仿真】(十五)指令系统位操作类指令 — 位运算指令、位条件转移指令
LETV has more than 400 employees? Living a fairy life without a boss, the official responded
【单片机仿真】(十六)控制转移类指令 — 无条件转移指令、条件转移指令
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
无线用的鉴权代码
Affine transformation implementation
Fiddler grabbing
[MCU simulation] (XVI) control transfer instructions - unconditional transfer instructions, conditional transfer instructions
05 central processing unit