当前位置:网站首页>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
边栏推荐
- MySQL面试题(2022)
- 04_ Service registration Eureka
- [MCU simulation] (XV) instruction system bit operation instructions - bit operation instructions, bit conditional transfer instructions
- [MCU simulation] (VIII) instruction system - data transmission instruction
- 【单片机仿真】(十六)控制转移类指令 — 无条件转移指令、条件转移指令
- All dates in Oracle query time period
- 【单片机仿真】(十九)介绍汇编、汇编指令、伪指令
- [MCU simulation] (XVIII) control transfer instructions - empty operation instructions
- ncnn DataReader&Extractor&blob
- ncnn Allocator内存分配器
猜你喜欢

人脸检测几种方法

多项式插值拟合(二)

PyTorch最佳实践和代码模板
![[MCU simulation] (XX) org - set start address](/img/9e/4e44dd779b0de28a190d86fbb1c2c0.png)
[MCU simulation] (XX) org - set start address

05_服务调用Ribbon

Understand the JVM memory structure in one article

Oracle gets the last and first data (gets the first and last data by time)

通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动

Summary of the most complete methods of string interception in Oracle

人脸关键点检测
随机推荐
05 central processing unit
while 循环
LETV has more than 400 employees? Living a fairy life without a boss, the official responded
多表查询——案例练习
JDBC连接Mysql数据库
MySQL multi table query
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
ncnn 部分算子不支持的替换操作
Oracle gets the last and first data (gets the first and last data by time)
Redis和其他数据库的比较
Detailed explanation of case when usage of SQL
【剑指Offer】31-35题(判断一个序列是否是栈的出栈序列之一,层序打印二叉树以及分行打印、每行逆着打印),判断序列是否是二叉搜索树的后序遍历路径,二叉树找一条权值为K的路径,复制复杂链表
4年开发二面美团最终败给:volatile关键字作用和原理这道面试题
2022-07-16:以下go语言代码输出什么?A:[];B:[5];C:[5 0 0 0 0];D:[0 0 0 0 0]。 package main import ( “fmt“ )
4. Some thoughts on asynctool framework
【单片机仿真】(十三)指令系统逻辑运算指令 — 移位指令
PyTorch最佳实践和代码模板
BiSeNetV2-面部分割
Specifications, multi table query basis
Need to slow down a little