Linux信号量 sem
#include <semaphore.h>
static sem_t sem_out[16];
static sem_t sem_in[16];
static struct timespec ts;
int Exxxx_Open(unsigned short mc,short ip3,short ip4)
{
.....
pthread_create(&tid[mc],nullptr,ProcCtrlRecv0,&mc );
sem_init(&sem_out[mc],0,0);
sem_init(&sem_in[mc],0,0);
}
int Exxxx_Close(unsigned short mc)
{
......
sem_close(&sem_out[mc]);
sem_close(&sem_in[mc]);
.......
}
int Send_to(unsigned char mc,unsigned char com)
{
sendto( sockfd[mc] , (const char *)&command[mc][0], 8, 0, (sockaddr*)&addrCtrl[mc], sizeof(sockaddr));
sem_post(&sem_out[mc]);
clock_gettime(CLOCK_REALTIME,&ts);
ts.tv_sec +=1;
ret=sem_timedwait(&sem_in[mc],&ts);
......
}
void* ProcCtrlRecv0(void *args)
{
while(m_bNoExit[mc])
{
.......
sem_wait(&sem_out[mc]);
nRet = recvfrom(sockfd[mc], InBuf, 8,0,(struct sockaddr*)&addrCtrl[mc], &addrServer_length);
......
sem_post(&sem_in[mc]);
......
}
}
以下摘录:
windows的WaitForSingleObject这个接口超级混乱,
等线程用 pthread_join,
等semphore用 sem_wait
等mutex用 pthread_mutex_lock
windows偏要做大而全, 让人很无奈.
以下引用:
Linux信号量 sem_t简介
函数sem_wait, sem_timedwait, sem_trywait比较
sem_wait()和sem_timedwait()区别
sem_timedwait() 简单用法
epoll
epoll简介(一)
epoll_create和epoll_create1
一个简单的基于epoll的udp接收
互斥锁
linux下用互斥锁
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr_t *attr); /初始化函数/
int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);/去初始化函数/
int pthread_mutex_lock(pthread_mutexattr_t *attr)/加锁/
int pthread_mutex_unlock(pthread_mutexattr_t *attr)/解锁/
.
linux线程互斥量pthread_mutex_t使用简介
互斥锁pthread_mutex_init()函数