Team OS/2 Italia - Developer Assistant

------------------------------------------

Programmazione Multi-Threading


#define INCL_DOSPROCESS
#define INCL_DOSEXCEPTIONS
#define INCL_DOSSEMAPHORES

#include <os2.h>
#include <stdio.h>
#include <process.h>


/* Prototipi delle funzioni */


ULONG APIENTRY ThreadException(PEXCEPTIONREPORTRECORD,
                               PEXCEPTIONREGISTRATIONRECORD,
                               PCONTEXTRECORD,
                               PVOID);

void Thread1(void *);

void main(void)
{
    EXCEPTIONREGISTRATIONRECORD		ExceptionReg={0,ThreadException};

    /* L'EXCEPTIONREGISTRATIONRECORD deve stare sullo stack del thread che lo
       registra */

    
	APIRET		rc;
	TID			tid1;
	TID			tid2;

	DosSetExceptionHandler(&ExceptionReg);
    
    tid1=_beginthread(Thread1,NULL,0x2000,(void *)100);

    /* Crea e fa partire il primo thread, lo stack viene allocato dal SO 
     Il parametro da passare al thread dovrebbe essere un puntatore ad 
     un qualsiasi tipo di struttura dati, in questo caso pero` l'ho usato
     come variabile intera forzandolo */

    printf("Created thread n.%d\n",tid1);
    
    tid2=_beginthread(Thread1,NULL,0x2000,(void *)50);
	printf("Created thread n.%d\n",tid2);
    
    DosSleep(500);
    DosKillThread(tid1);
        
    DosSleep(8000);
    
    DosUnsetExceptionHandler(&ExceptionReg);

    /* Toglie il nostro gestore delle eccezioni dalla catena, altrimenti il 
       meccanismo delle eccezioni non puo` funzionare piu` correttamente */

    _endthread();
}


void Thread1(void *limit)
{
	EXCEPTIONREGISTRATIONRECORD		ExceptionReg={0,ThreadException};
	PPIB	pPib;
	PTIB	pTib;
	int		i,j;

	DosSetExceptionHandler(&ExceptionReg);
	DosGetInfoBlocks(&pTib,&pPib);	

	printf("Started thread n.%d in process %d\n",pTib->tib_ptib2->tib2_ultid,
           pPib->pib_ulpid);
	
	i=j=0;
	while(j<8)
	{
		if(i++>(int)limit)
		{
			printf("Thread n.%d Count: %d\n",pTib->tib_ptib2->tib2_ultid,j++);
            i=0;
		}
	}
			
	DosUnsetExceptionHandler(&ExceptionReg);
    _endthread();

    /* Conclude soltanto il thread corrente */

}

ULONG APIENTRY ThreadException(PEXCEPTIONREPORTRECORD pReport,
                               PEXCEPTIONREGISTRATIONRECORD pReg,
                               PCONTEXTRECORD pContext,
                               PVOID pv)
{
	PPIB	pPib;
	PTIB	pTib;

	DosGetInfoBlocks(&pTib,&pPib);

	switch(pReport->ExceptionNum)
	{
        case XCPT_PROCESS_TERMINATE:
            printf("terminate.\n");
            break;
        case XCPT_ASYNC_PROCESS_TERMINATE:
            printf("Async _endthread()->");
            _endthread();

            /* Libera le risorse allocate dalla libreria per il thread.
             Non proseguira` nel default in quanto la _endthread() 
             chiama DosExit(EXIT_THREAD,0) che genera subito un
             XCPT_PROCESS_TERMINATE */

		default:
			printf("Thread: %d Exception: %X\n",pTib->tib_ptib2->tib2_ultid,
								      		    pReport->ExceptionNum);
			break;
	}
	return XCPT_CONTINUE_SEARCH;
}

---------------------------------------------------

Torna alla nostra HomePage - Pagina a cura di: Giovanni Pagni
Last modified 24-10-97