Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [C] Thread

    Ciao ragazzi...

    Vorrei gestire i thread di un mio programma
    tramite un modulo. Quindi dovrei scrivere
    il modulo.

    Il mio problema sorge perchè vorrei creare
    qualcosa di generico. Il mio intento è quello di
    poter creare thread puntando a funzioni generiche
    che io non conosco. Infatti la funzione potrebbe
    restituire un valore oppure avere N parametri.
    Come si può risolvere?

    codice:
    HANDLE hThrd[MAX_THRD];
    DWORD dwThrdId[MAX_THRD];
    
    int createThrd( TIPO MIA_FUNZIONE(PAR1,PAR2,PARn), int id ) {
    hThrd[id] = CreateThread(NULL, 0, COME SI FA ORA? , dwThrdId[id]);
    return hThrd[id] ? 1 : 0;
    }

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Intanto è meglio se utilizzi la beginthreadex, perchè chiamare direttamente l'api di WIN (se lo usi), genera dei memory leak.

    Poi la funzione da mettere in thread è definita con un prototipo fisso che prende un parametro void* e restituisce un DWORD, per passare i dati quindi puoi utilizzare un puntatore ad una struct che raccoglie i parametri che vuoi passare.

  3. #3
    Puoi farmi un esempio? Non credo d'aver capito.
    A me bastano anche due righe di codice e poi modifico e sviluppo
    da solo...

    Grazie per la risposta. ;-)

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Questo esempio vale per windows (se usi linux il procedimento dovrebbe essere più o meno uguale credo)

    codice:
    struct MyParam {
    	/* quel che serve */
    
    };
    
    
    /* funzione da mandare in multi threading /*
    unsigned int WINAPI threadFunction(void* value) {
    	if (value == NULL) return 1;
    	struct MyParam* myparam = (struct MyParam*) value;
    	/* ... */
    }
    
    HANDLE hThread;
    unsigned int thrdId;
    struct MyParam threadFunctionParameters;
    
    int main() {
    
    	hthread = (HANDLE) _beginthreadex(NULL,0,threadFunction,&threadFunctionParameters,0, &thrdId);
    	WaitForSingleObject(hThread,INFINITE);
    
    }

  5. #5
    Ti ringrazio... Si uso anche Linux, però questo progetto
    è destinato all'ambiente Win32.

    Ah, cosa intendi per Memory Leak ??

  6. #6
    Scusa ma ho corretto la mia pigrizia:
    http://it.wikipedia.org/wiki/Memory_leak

  7. #7
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Per quanto riguarda i possibili memory-leak, la documentazione di CreateThread (vedi qui) dice:

    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.