当前位置: 技术问答>linux和unix
c++程序移植到Linux下线程问题请教
来源: 互联网 发布时间:2016-09-25
本文导语: int Si_OpenDevice(int sTimeoutSecond) { memset(g_DeviceStates,DEVICE_NOTEXIST,MAX_CARD_NUM); g_DriverType = Si_JudgeDevice(); //创建互斥 DWORD nWaitResult=0; RW_Device=CreateMutex(NULL,true,MUTEX_GUID); nWaitResult=GetLastError(); if(nWaitResult== ERROR_ALREADY_EX...
int Si_OpenDevice(int sTimeoutSecond)
{
memset(g_DeviceStates,DEVICE_NOTEXIST,MAX_CARD_NUM);
g_DriverType = Si_JudgeDevice();
//创建互斥
DWORD nWaitResult=0;
RW_Device=CreateMutex(NULL,true,MUTEX_GUID);
nWaitResult=GetLastError();
if(nWaitResult== ERROR_ALREADY_EXISTS)
RW_Device=OpenMutex(MUTEX_ALL_ACCESS,true,MUTEX_GUID);
ReleaseMutex(RW_Device);
if(g_DriverType == NONE_VERSION)
return SR_CANNOT_OPEN_DEVICE;
if(g_DriverType == SJW_1_8_6_11)
return Si_OpenDevice2("\\.\SJW16_skt",sTimeoutSecond);
if(g_DriverType == SJW_1_8_6_15)
return Si_OpenDevice1("\\.\SJW16_skt",sTimeoutSecond);
if(g_DriverType == RSA_1_8_6_15)
return Si_OpenDevice1("\\.\RSA_skt",sTimeoutSecond);
else
return SR_CANNOT_OPEN_DEVICE;
return SR_SUCCESSFULLY;
}
最主要是这句RW_Device=CreateMutex(NULL,true,MUTEX_GUID);
我知道在Linux下要用这个pthread_mutex_init(&mutex, NULL))函数初始化但是我不知道 参数怎么弄
#define MUTEX_GUID "{Monopolization-Sjw16}"
MUTEX_GUID 这个参数放在哪 望好心人指点一二,万分感谢!
{
memset(g_DeviceStates,DEVICE_NOTEXIST,MAX_CARD_NUM);
g_DriverType = Si_JudgeDevice();
//创建互斥
DWORD nWaitResult=0;
RW_Device=CreateMutex(NULL,true,MUTEX_GUID);
nWaitResult=GetLastError();
if(nWaitResult== ERROR_ALREADY_EXISTS)
RW_Device=OpenMutex(MUTEX_ALL_ACCESS,true,MUTEX_GUID);
ReleaseMutex(RW_Device);
if(g_DriverType == NONE_VERSION)
return SR_CANNOT_OPEN_DEVICE;
if(g_DriverType == SJW_1_8_6_11)
return Si_OpenDevice2("\\.\SJW16_skt",sTimeoutSecond);
if(g_DriverType == SJW_1_8_6_15)
return Si_OpenDevice1("\\.\SJW16_skt",sTimeoutSecond);
if(g_DriverType == RSA_1_8_6_15)
return Si_OpenDevice1("\\.\RSA_skt",sTimeoutSecond);
else
return SR_CANNOT_OPEN_DEVICE;
return SR_SUCCESSFULLY;
}
最主要是这句RW_Device=CreateMutex(NULL,true,MUTEX_GUID);
我知道在Linux下要用这个pthread_mutex_init(&mutex, NULL))函数初始化但是我不知道 参数怎么弄
#define MUTEX_GUID "{Monopolization-Sjw16}"
MUTEX_GUID 这个参数放在哪 望好心人指点一二,万分感谢!
|
linux下线程是按POSIX标准来的,里面没有GUID这个东西,这是windows下管理对象的一个概念。使用很简单,
pthread_mutex m;
if (pthread_mutex_init(&m, NULL) != 0)
...// error handle
pthread_mutex_lock(&m);
...
pthread_mutex_unlock(&m);
...
pthread_mutex_destroy(&m);
pthread_mutex m;
if (pthread_mutex_init(&m, NULL) != 0)
...// error handle
pthread_mutex_lock(&m);
...
pthread_mutex_unlock(&m);
...
pthread_mutex_destroy(&m);