» »

[c++] Dve niti noceta komunicirati

[c++] Dve niti noceta komunicirati

goranv ::

Namen je ustvariti dve niti, katerima posljem njen id in od druge niti. Nato vsaka nit sama sebi poslje dva msg. Problem je nobena nit mi nic ne izpise, razen ce dodam Sleep( 10 ) v funkciji Start() dobim samo dve sporocili
[id: 0] ldwOtherId: 8844
[id: 0] ldwOtherId: 9164





#define THRD_MESSAGE_START				WM_USER + 1
#define THRD_MESSAGE_SET				WM_USER + 2
#define THRD_MESSAGE_GET				WM_USER + 3
#define THRD_MESSAGE_END				WM_USER + 4

#define THRD_WPARAM_SET_OTHER_ID		1
#define THRD_WPARAM_SET_OWN_ID			2


HANDLE	ghMutex;
LPCTSTR lpszMutex = L"enassa";



DWORD	ThreadFunc(		_In_opt_	PVOID	lp	)
{
	MSG		lmsg;
	DWORD	ldwOwnId	= 0,
			ldwOtherId	= 0;
	bool	lInitFinish	= false,
			ldone		= false;

	HANDLE	hMutex	= OpenMutex( MUTEX_ALL_ACCESS, FALSE, lpszMutex );


	while( !ldone )
	{
		while( GetMessage( &lmsg, 0, THRD_MESSAGE_START, THRD_MESSAGE_END ) > 0 )
		{
			switch( lmsg.message )
			{
				case THRD_MESSAGE_START:

					if( lInitFinish )
					{
						WaitForSingleObject( hMutex, INFINITE );
						printf( "[id: %d] [%d]\n", ldwOwnId, GetTickCount() );
						ReleaseMutex( hMutex );

						ldone	= true;
					}

					break;

				case THRD_MESSAGE_SET:

					switch( lmsg.wParam )
					{
						case THRD_WPARAM_SET_OTHER_ID:

							ldwOtherId	= (DWORD)lmsg.lParam;

							WaitForSingleObject( hMutex, INFINITE );
							printf( "[id: %d] ldwOtherId: %u\n", ldwOwnId, ldwOtherId );
							ReleaseMutex( hMutex );

							if( ldwOwnId != 0 )
								PTMSG( ldwOtherId, THRD_MESSAGE_GET, TRUE, 0 );

							break;

						case THRD_WPARAM_SET_OWN_ID:

							ldwOwnId	= (DWORD)lmsg.lParam;

							WaitForSingleObject( hMutex, INFINITE );
							printf( "[id: %d] ldwOwnId: %u\n", ldwOwnId, ldwOwnId );
							ReleaseMutex( hMutex );

							if( ldwOtherId != 0 )
								PTMSG( ldwOtherId, THRD_MESSAGE_GET, TRUE, 0 );

							break;
					}

					break;

				case THRD_MESSAGE_GET:

					// osnovne vrednosti nastavljene
					if( lmsg.wParam == TRUE )
					{
						lInitFinish	= true;

						WaitForSingleObject( hMutex, INFINITE );
						printf( "[id: %d] lInitFinish: %d\n", ldwOwnId, lInitFinish );
						ReleaseMutex( hMutex );

						PTMSG( ldwOwnId, THRD_MESSAGE_START, 0, 0 );
					}

					break;

				case THRD_MESSAGE_END:
					break;
			}

			Sleep( 1 );
		}
	}


	WaitForSingleObject( hMutex, INFINITE );
	printf( "[id: %d] ExitThread\n", ldwOwnId );
	ReleaseMutex( hMutex );

	CloseHandle( hMutex );
	ExitThread( 0 );
}



Test1 :: Test1()
{
	pHandles[0]	= pHandles[1]	= NULL;
}

void	Test1 :: Start()
{
// private:
//	Thread		thr[2];
//	HANDLE		pHandles[2];


	ghMutex	= CreateMutex( NULL, FALSE, lpszMutex );
	if( ghMutex == NULL )
		printf( "ghMutex\n" );

	thr[0].Create( ThreadFunc );
	thr[1].Create( ThreadFunc );

	thr[0].Send( THRD_MESSAGE_SET, THRD_WPARAM_SET_OWN_ID, thr[0].GetId() );
	thr[1].Send( THRD_MESSAGE_SET, THRD_WPARAM_SET_OWN_ID, thr[1].GetId() );
	Sleep( 10 );

	thr[0].Send( THRD_MESSAGE_SET, THRD_WPARAM_SET_OTHER_ID, thr[1].GetId() );
	thr[1].Send( THRD_MESSAGE_SET, THRD_WPARAM_SET_OTHER_ID, thr[0].GetId() );
	Sleep( 10 );

	pHandles[0]	= thr[0].GetHandle();
	pHandles[1]	= thr[1].GetHandle();

	WaitForMultipleObjects( 2, pHandles, TRUE, -1 );

	CloseHandle( ghMutex );
}


////////////////////////////////////////////
// Thread.h
////////////////////////////////////////////


typedef struct ThreadInfo
{
	HANDLE		h;			// handle
	DWORD		id;			// id thread
	PVOID		lp;			// lparam thread
} THREADINFO, *PTHREADINFO;



class Thread
{
	ThreadInfo		ti;

public:

	 Thread();
	~Thread();

	/**
		@param nFlags, 0 - run, CREATE_SUSPENDED - paused
	*/
	_Success_( return == true )
	bool	Create(

		_In_		LPTHREAD_START_ROUTINE		pFunc,
		_In_opt_	PVOID						nParam	= nullptr,
		_In_opt_	DWORD						nFlags	= 0			);

	void	Send(

		_In_	DWORD		ndwMsg,
		_In_	WPARAM		nwParam,
		_In_	LPARAM		nlParam		) const;

	_Success_( return > 0 )		DWORD	GetId() const;

	_Success_( return > 0 )		HANDLE	GetHandle() const;
};


////////////////////////////////////////////
// Thread.cpp
////////////////////////////////////////////


Thread :: Thread()
{
	ti.h	= NULL;
	ti.id	= 0;
	ti.lp	= 0;
}

Thread :: ~Thread()
{
	if( ti.h != NULL )
	{
		CloseHandle( ti.h );
		
		ti.h	= NULL;
		ti.id	= 0;

		if( ti.lp != nullptr )
		{
			HeapFree( GetProcessHeap(), 0, ti.lp );
			ti.lp	= 0;
		}
	}
}

_Success_( return == true )
bool	Thread :: Create(

	_In_		LPTHREAD_START_ROUTINE		pFunc,
	_In_opt_	PVOID						nParam,
	_In_opt_	DWORD						nFlags	)
{
	ti.h	= CreateThread( nullptr, 0, pFunc, nParam, nFlags, &ti.id );
	if( ti.h != NULL )
	{
		return true;
	}
	
	return false;
}

void	Thread :: Send(

		_In_	DWORD		ndwMsg,
		_In_	WPARAM		nwParam,
		_In_	LPARAM		nlParam		) const
{
	PTMSG( ti.id, ndwMsg, nwParam, nlParam );
}

_Success_( return > 0 )
DWORD	Thread :: GetId() const
{
	return ti.id;
}

_Success_( return > 0 )
HANDLE	Thread :: GetHandle() const
{
	return ti.h;
}


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[Java] Objekt poslan k metodi v kakšnem stanju?

Oddelek: Programiranje
211564 (1011) shadeX
»

slovenski prevodi programerskih izrazov

Oddelek: Programiranje
494169 (3104) sverde21
»

Komunikacija med thread-i

Oddelek: Programiranje
133759 (3565) zlatko
»

OpenGL orr v ansi c

Oddelek: Programiranje
121648 (1436) igor12
»

Odpiranje dat.exe v VB

Oddelek: Programiranje
123004 (2797) webblod

Več podobnih tem