求win32多线程api转换为linux的具体实现代码
rt!~
我现在需要对一个win32程序转化到linux下,在多线程问题上一直止步不前,高分求相应的具体实现,小弟在此谢过了~稍后将我的代码贴上~望各位指点以下~谢谢了~
我现在重写的多线程函数主要有以下几个:SetEvent, ResetEvent, WaitForSingleObject, WaitForMultipleObjects!在我调试的过程中,总是在pthread_cond_timedwait时一直等待,但从我的跟踪可以看出,已经设定了事件了的~但依然阻塞,参考了很多的例程,不明白怎么回事~同时我的时间设了只有1ms。各位师兄是否有相关经验的指点一下,谢谢了
另外我现在的linux环境是uclinux
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另在使用gdb的时候如何进行多线程调试呢?我现在gdb使用多线程不能调试,捕获不到断点
下面是我的代码,在几个板块都贴了,没有人回答阿~贴上我的,大家看哈有什么问题啊
static void gettimespec(void *ptimespec, int offset)
{
timeval now;
timespec *ptime = (timespec *)ptimespec;
gettimeofday(&now, NULL);
ptime->;tv_sec = now.tv_sec;
int tmp = now.tv_usec + offset * 1000;
if(tmp < 1000 * 1000)
ptime->;tv_nsec = tmp * 1000;
else if(tmp < 2 * 1000 * 1000)
{
ptime->;tv_sec++;
ptime->;tv_nsec = (tmp - 1000 * 1000) * 1000;
}
else
{
int s = tmp / (1000 * 1000);
ptime->;tv_sec += s;
ptime->;tv_nsec = (tmp - s * 1000 * 1000) * 1000;
}
}
void init_recursive_mutex(void *m)
{
static const pthread_mutex_t mutex_r = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
memcpy(m, &mutex_r, sizeof(mutex_r));
}
void init_normal_mutex(void *m)
{
static const pthread_mutex_t mutex_n = PTHREAD_MUTEX_INITIALIZER;
memcpy(m, &mutex_n, sizeof(mutex_n));
}
class EventCOND
{
private:
int eventcount;
pthread_cond_t cond;
pthread_mutex_t wait_mutex;
pthread_mutex_t event_mutex;
pthread_mutex_t single_lock;
public:
EventCOND()
{
eventcount = 0;
pthread_cond_init(&cond, NULL);
init_recursive_mutex(&event_mutex);
pthread_mutex_init(&wait_mutex, NULL);
init_recursive_mutex(&single_lock);
}
~EventCOND()
{
pthread_cond_destroy(&cond);
}
void SetEvent();
void ResetEvent();
DWORD WaitSingle(DWORD dwMilliseconds);
};
void EventCOND::SetEvent()
{
pthread_mutex_lock(&event_mutex);
eventcount++;
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&event_mutex);
}
void EventCOND::ResetEvent()
{
pthread_mutex_lock(&event_mutex);
pthread_cond_destroy(&cond);
pthread_cond_init(&cond, NULL);
eventcount = 0;
pthread_mutex_unlock(&event_mutex);
}
DWORD EventCOND::WaitSingle(DWORD dwMilliseconds)
{
int code;
timespec timeout;
pthread_mutex_lock(&single_lock);
code = 0;
while(eventcount <= 0 && code != ETIMEDOUT)
{
if(dwMilliseconds == INFINITE)
{
pthread_mutex_lock(&wait_mutex);
code = pthread_cond_wait(&cond, &wait_mutex);
pthread_mutex_unlock(&wait_mutex);
}
else
{
pthread_mutex_lock(&wait_mutex);
gettimespec(&timeout, dwMilliseconds);
code = pthread_cond_timedwait(&cond, &wait_mutex, &timeout);
pthread_mutex_unlock(&wait_mutex);
}
}
pthread_mutex_unlock(&single_lock);
return code;
}
HANDLE CreateEvent(LPSECURITY_ATTRIBUTES lpEventAttributes, BOOL bManualReset,
BOOL bInitialState, LPCTSTR lpName)
{
EventCOND *pnew_event = new EventCOND;
return pnew_event;
}
BOOL SetEvent(HANDLE hEvent)
{
if(hEvent == NULL)
return FALSE;
EventCOND *pEvent = (EventCOND *)hEvent;
pEvent->;SetEvent();
return TRUE;
}
BOOL ResetEvent(HANDLE hEvent)
{
if(hEvent == NULL)
return FALSE;
EventCOND *pEvent = (EventCOND *)hEvent;
pEvent->;ResetEvent();
return TRUE;
}
BOOL CloseHandle(HANDLE hEvent)
{
if(hEvent == NULL)
return TRUE;
EventCOND *pEvent = (EventCOND *)hEvent;
delete pEvent;
pEvent = NULL;
return TRUE;
}
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
{
if(hHandle == NULL)
return 0;
EventCOND *pEvent = (EventCOND *)hHandle;
return pEvent->;WaitSingle(dwMilliseconds);
}
DWORD WaitForMultipleObjects(DWORD Count, CONST HANDLE *lpHandles,
BOOL bWaitAll, DWORD dwMilliseconds)
{
const int META = 1;
int code, off, i;
if(bWaitAll)
{
return -1;
}
code = off = i = 0;
EventCOND **ppEvent = (EventCOND **)lpHandles;
while(1)
{
code = ppEvent->;WaitSingle(META);
if(code != ETIMEDOUT)
break;
i++;
if(i != Count)
continue;
i = 0;
// Sleep(META);
if(dwMilliseconds == INFINITE)
continue;
off += META * Count;
if(off >;= dwMilliseconds)
return ETIMEDOUT;
}
return i;
}