---------------------------- 初始化临界区 ----------------------------------
(win)
InitializeCriticalSection(RTL_CRITICAL_SECTION &rtl_critial_section)
(linux)
pthread_mutexattr_init(&(mutex)->attr);
pthread_mutexattr_settype(&(mutex)->attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&(mutex)->mtx, &(mutex)->attr);
---------------------------- 删除临界区 ----------------------------
(win)
DeleteCriticalSection(RTL_CRITICAL_SECTION &)
(linux)
pthread_mutex_destroy(pthread_mutex_t &mutex)
---------------------------- 进入临界区 ----------------------------
(win)
EnterCriticalSection(RTL_CRITICAL_SECTION &rtl_critical_section)
(linux)
pthread_mutex_lock(pthread_mutex_t &mutex)
---------------------------- 尝试进入临界区 ----------------------------
(win)
TryEnterCriticalSection(RTL_CRITICAL_SECTION &rtl_critical_section )
(linux)
pthread_mutex_trylock(pthread_mutex_t &mutex)
---------------------------- 离开临界区 ----------------------------
(win)
LeaveCriticalSection(RTL_CRITICAL_SECTION &rtl_critical_section )
(linux)
pthread_mutex_unlock(pthread_mutex_t &mutex)
------------------------------------------------------------------------------------------
把目标操作数(第1参数所指向的内存中的数)与一个值(第3参数)比较,如果相等,则用另一个值(第2参数)与目标操作数(第1参数所指向的内存中的数)交换;InterlockedExchange是不比较直接交换。整个操作过程是锁定内存的,其它处理器不会同时访问内存,从而实现多处理器环境下的线程互斥
(win)
InterlockedCompareExchange(Destination, newvalue, oper)
(linux)
__sync_val_compare_and_swap(Destination, oper, newvalue)
---------------------------- v的值原子添加P的大小 ----------------------------
(win)
InterlockedExchangeAdd(V, P)
(linux)
__sync_fetch_and_add(V, P)
---------------------------- 原子增加一 ----------------------------
(win)
InterlockedIncrement(T)
(linux)
__sync_fetch_and_add(T, 1)
---------------------------- 原子减少一 ----------------------------
(win)
InterlockedDecrement(T)
(linux)
__sync_fetch_and_sub(T, 1)
---------------------------- 获取当前线程id ----------------------------
(win)
GetCurrentThreadId()
(linux)
syscall(SYS_gettid)
------------------------------------------------------------------------------------
如果指定一个非零值,函数处于等待状态直到hHandle 标记的对象被触发,或者时间到了。如果dwMilliseconds 为0,对象没有被触发信号,函数不会进入一个等待状态,它总是立即返回。如果dwMilliseconds 为INFINITE,对象被触发信号后,函数才会返回。对应的linux实现使用条件变量
(win)
WaitForSingleObject(event,INFINITE)
(linux)
pthread_mutex_lock( &m_tx );
pthread_cond_wait( &event, &m_tx );
pthread_mutex_unlock( &m_tx );
---------------------------- 退出线程(退出参数0) ----------------------------
(win)
ExitThread(0)
(linux)
pthread_exit(0)
------------------------------------------------------------------------------------
设置线程优先级,pthread_setschedparam在多线程开发中经常被使用的,它主要用于设置线程的调用策略和优先级
(win)
SetThreadPriority (handle,nPrioroty)
(linux)
sched_param sp = {nPriority};
if(0 == pthread_setschedparam(m_pid, SCHED_RR, &sp))
{
return true;
}
return false;
---------------------------- 获取优先级 ----------------------------
(win)
GetThreadPriority( Handle m_hThread )
(linux)
int policy;
sched_param sp;
pthread_getschedparam(m_pid, &policy, &sp))
sp.sched_priority;
---------------------------- 初始化互斥量 ----------------------------
(linux)
pthread_mutex_init(pthread_mutex_t &mutex),0)
---------------------------- 初始化条件变量 ----------------------------
pthread_cond_init(&cond,0)
---------------------------- 删除互斥量 ----------------------------
pthread_mutex_destroy(pthread_mutex_t &mutex))
---------------------------- 删除条件变量 ----------------------------
(linux)
pthread_cond_destroy(pthread_cond_t &cond)
---------------------------- 向条件变量发起信号 ----------------------------
即通知一个线程,线程条件发生
(win)
SetEvent(handle)
(linux)
pthread_cond_signal(pthread_cond_t &cond)//通知一个线程,线程条件发生
pthread_cond_broadcast(pthread_cond_t &cond)//通知所有线程,线程条件发生
------------------------------------------------------------------------------------
挂起等待结束(无限等待) true在阻塞期间允许进入警告状态(windows才有)
(win)
WaitForSingleObject Ex(handle, INFINITE,true)
(linux)
pthread_join(pthread_t thid, void ** ret_val) 常用pthread_join(pid,0)
附:
Windows中的WaitForSingleObject()函数对应在Linux中的sem_wait(),SetEvent对应sem_post(),
Windows中的WaitForSingleObject()函数对应在vxworks中semTake(),SetEvent对应semGive().
线程同步之信号量(sem_init,sem_post,sem_wait)
信号量和互斥锁(mutex)的区别:互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区。要使用信号量同步,需要包含头文件semaphore.h。
主要用到的函数:
int sem_init(sem_t *sem, int pshared, unsigned int value);,其中sem是要初始化的信号量,pshared表示此信号量是在进程间共享还是线程间共享,value是信号量的初始值。
int sem_destroy(sem_t *sem);,其中sem是要销毁的信号量。只有用sem_init初始化的信号量才能用sem_destroy销毁。
int sem_wait(sem_t *sem);等待信号量,如果信号量的值大于0,将信号量的值减1,立即返回。如果信号量的值为0,则线程阻塞。相当于P操作。成功返回0,失败返回-1。
int sem_post(sem_t *sem); 释放信号量,让信号量的值加1。相当于V操作。
对象 | 操作 | Linux Pthread API | Windows SDK 库对应 API |
线程 | 创建 | pthread_create | CreateThread |
退出 | pthread_exit | ThreadExit | |
等待 | pthread_join | WaitForSingleObject | |
互斥锁 | 创建 | pthread_mutex_init | CreateMutex |
销毁 | pthread_mutex_destroy | CloseHandle | |
加锁 | pthread_mutex_lock | WaitForSingleObject | |
解锁 | pthread_mutex_unlock | ReleaseMutex | |
条件 | 创建 | pthread_cond_init | CreateEvent |
销毁 | pthread_cond_destroy | CloseHandle | |
触发 | pthread_cond_signal | SetEvent | |
广播 | pthread_cond_broadcast | SetEvent或ResetEvent | |
等待 | pthread_cond_wait或pthread_cond_timedwait | SingleObjectAndWait |