| |

VerySource

 Forgot password?
 Register
Search
View: 2446|Reply: 7

How to achieve Windows-like WaitForMultipleObjects function under Linux

[Copy link]

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-12 22:30:01
| Show all posts |Read mode
code show as below:
HANDLE handles [2] = {m_quitEvent.GetHandle (), m_semaphore.GetHandle ()};
UInt32 uWait = WaitForMultipleObjects (2, handles, False, MAX_TIMEOUT);
if (uWait-WAIT_OBJECT_0 == 0)
{
// Exit signal
break;
}


How does Linux code work?
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-4-10 11:30:01
| Show all posts
code show as below:
HANDLE handles [2] = {m_quitEvent.GetHandle (), m_semaphore.GetHandle ()};
UInt32 uWait = WaitForMultipleObjects (2, handles, False, MAX_TIMEOUT);
if (uWait-WAIT_OBJECT_0 == 0)
{
// exit signal
break;
}


How to implement Linux code?
Reply

Use magic Report

1

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-6-11 09:15:01
| Show all posts
Can sig_wait?
Reply

Use magic Report

1

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-27 09:45:01
| Show all posts
while(True)
{
//Exit the signal, wait for 1 millisecond
if(m_quitEvent.Wait(1))
break;

//There is a signal
if(m_semaphore.TryLock(0))
break;
}
I am now using the above code to simulate and implement it, basically achieving a function similar to WaitForMultipleObjects;

Ask if there is any simpler function to replace;
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 18:45:02
| Show all posts
I implemented the win32 de thread synchronization mechanism under Linux, haha! ! Narcissism
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-2 19:00:01
| Show all posts
#ifndef _M_THREAD_BASEOBJ_DEF_H_
#define _M_THREAD_BASEOBJ_DEF_H_
#include <pthread.h>
#include <semaphore.h>
#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>

#include <Carbon/Carbon.h>
class MCritSec
{
private:
pthread_mutex_t m_mutex;
public:
MCritSec()
{
pthread_mutex_init (&m_mutex,NULL);
}
virtual ~MCritSec()
{
pthread_mutex_destroy(&m_mutex);
}
void Lock()
{
pthread_mutex_lock(&m_mutex);
}
void Unlock()
{
pthread_mutex_unlock(&m_mutex);
}
};

class MAutoLock
{
protected:
    MCritSec * m_pLock;

public:
    MAutoLock(MCritSec * plock)
    {
        m_pLock = plock;
        m_pLock->Lock();
    }

    ~MAutoLock()
{
        m_pLock->Unlock();
    }
};


class IDAllocator
{
typedef struct _IDNode
{
unsigned long ID;
_IDNode* Next;
}Node;
public:
IDAllocator();
virtual ~IDAllocator();
unsigned long GetID();
long ReleaseID(unsigned long nID);
To
private:
MCritSec m_Lock;
Node* m_pHead;
unsigned long m_nNextID;
int m_nLength;
unsigned long CalNextID(unsigned long nPreID);
To
To
};


#define MHANDLE_SUCCESS( T) ((long)T >=0)

enum
{
HANDLE_TYPE_BASE,
HANDLE_TYPE_MUTEX,
HANDLE_TYPE_THREAD,
HANDLE_TYPE_SEMAPHORE,
HANDLE_TYPE_EVENT
};

enum
{
HANDLE_STATE_THREADCREATE_SUSPEND=-1,
HANDLE_STATE_UNVALID=0,
HANDLE_STATE_VALID=1
To
};


enum
{
HANDLE_NOTIFYTYPE_SIGNAL,
HANDLE_NOTIFYTYPE_UNSIGNAL,
HANDLE_NOTIFYTYPE_UPDATA
};


class MHandleManager;





typedef struct _ManagerNode
{
MHandleManager* pMng;
_ManagerNode* Next;
}ManagerNode;

class MHBase
{
public:
MHBase();
virtual ~MHBase();
To
private:
static IDAllocator g_ObjIDAllocator;

protected:
int m_HandleType;
unsigned long m_HandleID;
unsigned long m_HandleState;


virtual long DelHeadManager();
virtual long DelManager(MHandleManager* pManager);
virtual long AddManager(MHandleManager* pManager);
virtual long NotifyManager(int NotifyType)=0;
To
ManagerNode* m_pMngListHead;
ManagerNode* m_pMngListTail;

MCritSec m_MngListLock;
MCritSec m_OPLock;


friend class MHandleManager;

};



typedef MHBase* MHANDLE;


class MEvent:public MHBase
{
public:
MEvent(bool InitState,bool IsManaul);
virtual ~MEvent();
void Set();
void Reset();
void Pulse();
private:
bool m_bManaulReset;
MCritSec m_Lock;
protected:
long NotifyManager(int NotifyType);
To
To
};




class MSemaphore:public MHBase
{
public:
MSemaphore(long nInitCounts,long nMaxCounts);
virtual ~MSemaphore();
long Signal(long nCounts=1);
private:
long m_nMaxSigCounts;
MCritSec m_Lock;
protected:
long NotifyManager(int NotifyType);
};



class MMutex:public MSemaphore
{
public:
MMutex(bool bInitState);
~MMutex();
};

typedef void*(*THREADFUNCPTR)(void*);
MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);

class MThread:public MHBase
{
typedef enum {CREATE_BYNEW,CREATE_BYFUNC}CREATE_TYPE;
public:
MThread();
virtual ~MThread();
To
long InitUsrThread(const pthread_attr_t *pAttr,void *pArg,bool IsCreateSuspend);
To

virtual long Resume();
virtual long Suspend(){return 0;};
int GetState(){return m_HandleState;}

protected:
CREATE_TYPE m_nCreateType;
To
pthread_t m_Thread;
pthread_attr_t m_ThreadAttribute;
void* m_pArg;
THREADFUNCPTR m_pThrdFunc;
To
MHANDLE m_evntRun;
To
protected:
static void* ThreadFunction(void* pv);
long NotifyManager(int NotifyType);
virtual long WorkFunc(void* pUsrData);

private:
long Init(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);


friend MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);

};



typedef struct _ID_INDEX_MAP
{
long objID;
bool bValid;

}IIMap;





class MHandleManager
{
public:
MHandleManager(MHANDLE* pHandles,int nCounts);
virtual ~MHandleManager();
long RecieveNotify(MHANDLE pHandle,int nNotifyType);
long WaitForHandles(bool bWaitForAll,long nWaitMillionSecond = 0x7FFFFFFF);
private:
To
MCritSec m_NotifyLock;
MCritSec m_ConstructLock;
//sem_t m_semComplete;
MPSemaphoreID m_semComplete;
To
MHANDLE* m_pHandle;
int m_nHandleCounts;
To
IIMap* m_pSignalInfoList;

int m_nNoSignalInfoCounts;
To
bool m_bTimeFlag;
bool m_bIsFirstSignal;
long m_lFirstSignalIndex;
To
//void WaitTimeOver();
To
To
friend class MHBase;
To
};

MHANDLE CreateEvent(bool bManaulReset,bool bInitState);
void SetEvent(MHANDLE event);
void ResetEvent(MHANDLE event);
void PulseEvent(MHANDLE event);
MHANDLE CreateSemaphore(long nInitSigs, long nMaxSigs);
void ReleaseSemaphore(MHANDLE semaphore,long nSigCounts,long* lRet=NULL);

MHANDLE CreateMutex(bool bInitState);
void ReleaseMutex(MHANDLE mutex);





MHANDLE CreateThread(const pthread_attr_t *pAttr, void *(*start_routine)(void *), void *pArg, bool IsCreateSuspend);
long ResumeThread(MHANDLE pthread);
long SuspendThread(MHANDLE pthread);

void CloseHandle(MHANDLE handle);


#define WAIT_FAILED -29296
long WaitForMultipleObjects(int nCounts,MHANDLE* pHandle,bool IsAll,long lMillionSeconds);
long WaitForSingleObject(MHANDLE Handle,long lMillionSeconds);






#endif //_M_THREAD_BASEOBJ_DEF_H_
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 16:30:01
| Show all posts
I also met!
The following is to return when there is a semaphore notification, and all semaphores are notified to the dish to return. This is similar
struct timespec mt={0,0};
while(1){
    if(sem_timedwait(signal1,&mt)){
         res = 0;break;
    }
    if(sem_timedwait(sinal2,&mt)){
         res = 1;break;
    }
}
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-5 16:45:01
| Show all posts
You can also use sem_trywait(); The return value 0 waits for success, -1 semaphore cannot respond immediately and fails.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list