Bonjour,
je fais mon premier programme utilisant le multi-threading sous windows.
J'ai un thread principal et un thread secondaire cr�� avec CreateThread() qui tourne en permanence avec une boucle while().
J'ai besoin de faire en sorte que mon thread principale appelle une fonction d'une classe instanci� dans le thread secondaire (qui modifie des �tats de cette classe).
Je peux utiliser pour cela la fonction PostThreadMessage() et faire executer la fonction d�sir�e par le second thread � la r�ception du message.
Mais mon probl�me, c'est que mon thread principal a besoin d'�tre certain que la fonction du thread secondaire a bien �t� execut� avant dans continuer son ex�cution (il doit attendre).
La seul solution que j'ai trouv� c'est quelque chose comme ci-dessous. Mais j'ai pas l'impression que ce soit la meilleur (surtout � cause du while de FonctionThreadPrincipal() si aucun message de retour n'est capt�)
Dans mon thread principal:
Dans mon thread secondaire:
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 void FonctionThreadPrincipal(int variable1, int variable2) { UINT msgSend = MESSAGE_PERSO; FONCTIONDESC* desc = new FONCTIONDESC(); desc->variable1 = variable1; desc->variable2 = variable2; WPARAM wParamSend = (WPARAM)desc; LPARAM lParamSend = (LPARAM)0; PostThreadMessage(m_ThreadSecondaireId,msgSend,wParamSend,lParamSend); // on attend MSG msgFeedBak; while(PeekMessage(&msgFeedBak,NULL,MESSAGE_PERSO,MESSAGE_PERSO,PM_REMOVE) == FALSE) { ; // on fait rien } // Ok on peut continuer. }
Qu'est ce que je dois faire ?
Code : S�lectionner tout - Visualiser dans une fen�tre � part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 while( runProcess ) { if( PeekMessage(&msg,NULL,MESSAGE_PERSO,MESSAGE_PERSO,PM_REMOVE) == TRUE ) { switch(msg.message) { case MESSAGE_PERSO: FONCTIONDESC* desc = (FONCTIONDESC*)msg.lParam; m_MonInstance->MaFonction( desc->variable1 ,desc->variable2); delete desc; // feedback UINT msgSend = MESSAGE_PERSO; WPARAM wParamSend = 0; LPARAM lParamSend = 0; PostThreadMessage(m_CallerThreadId,msgSend,wParamSend,lParamSend); break; }; } update(); }
Partager