2Fermer4
squalylLe 11/05/2009 à 17:22
comment dire... chez moi ton code tel que posté fonctionne bien.

et le même code qui fait un std::cout << "Hello" << std::endl; a la place du trycatch fonctionne très bien aussi, par contre tout ce que je peux dire c'est que les appels à cout .


ah c'est marrant le premier coup ça a marché mais maintenant quand je recompile le premier ça me met le même message que toi confus

en fait, ça marche mais pas à tous les coups.

SLO@SLO-PC1 ~
$ g++ test.cpp

SLO@SLO-PC1 ~
$ ./a.exe 
CREATE THREADS
START THREADS

SLO@SLO-PC1 ~
$ cp test.cpp test2.cpp

SLO@SLO-PC1 ~
$ notepad test2.cpp

SLO@SLO-PC1 ~
$ cat test.cpp
#include <iostream> 
#include <map> 
#include <windows.h> 
 
struct  MyException 
{ 
}; 
 
static DWORD WINAPI     MyCallback (LPVOID) 
{ 
        try 
        { 
                throw MyException (); 
        } 
        catch (MyException& e) 
        { 
        } 
 
        return 0; 
} 
 
int     main() 
{ 
        const int               count = 100; 
        std::map<int, HANDLE>   threads; 
        DWORD                   id; 
 
        std::cout << "CREATE THREADS" << std::endl; 
 
        for (int i = 0; i < count; ++i) 
                threads[i] = CreateThread (NULL, 0, &MyCallback, 0, CREATE_SUSPENDED, &id); 
 
        std::cout << "START THREADS" << std::endl; 
 
        for (int i = 0; i < count; ++i) 
                ResumeThread (threads[i]); 
 
        Sleep (1000); 
 
        return 0; 
}

SLO@SLO-PC1 ~
$ g++ test.cpp

SLO@SLO-PC1 ~
$ ./a.exe 
CREATE THREADS
START THREADS

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

SLO@SLO-PC1 ~
$ ./a.exe 
CREATE THREADS
START THREADS

SLO@SLO-PC1 ~
$ cat test2.cpp
#include <iostream> 
#include <map> 
#include <windows.h> 
 
struct  MyException 
{ 
}; 
 
static DWORD WINAPI     MyCallback (LPVOID) 
{ 
        std::cout << "Hello" << std::endl;
        return 0; 
} 
 
int     main() 
{ 
        const int               count = 100; 
        std::map<int, HANDLE>   threads; 
        DWORD                   id; 
 
        std::cout << "CREATE THREADS" << std::endl; 
 
        for (int i = 0; i < count; ++i) 
                threads[i] = CreateThread (NULL, 0, &MyCallback, 0, CREATE_SUSPENDED, &id); 
 
        std::cout << "START THREADS" << std::endl; 
 
        for (int i = 0; i < count; ++i) 
                ResumeThread (threads[i]); 
 
        Sleep (1000); 
 
        return 0; 
}

SLO@SLO-PC1 ~
$ g++ test2.cpp

SLO@SLO-PC1 ~
$ ./a.exe 
CREATE THREADS
START THREADS
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
HelloHelloHelloHelloHelloHello




Hello
Hello
HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello



Hello




Hello












Hello
Hello

Hello


HelloHelloHelloHelloHelloHelloHelloHelloHello
Hello
Hello
HelloHello
Hello
Hello





HelloHello
























Hello


Hello







HelloHelloHelloHello



Hello

SLO@SLO-PC1 ~



edit: après réflexion, je pense que le message d'erreur vient du fait que main() se termine alors qu'au moins un thread créé n'est pas terminé, du coup ça se finit "salement". C'est cohérent avec le pasplantage quand tu mets des petits sleep() qui permettent à chaque thread de se finir avant la fin du sleep, du coup à la fin de main ils sont tous terminés, ce qui n'est pas forcément le cas sans eux. 1 seconde de pause ça parait long, mais 100 threads en très peu de temps c'est sans doute un peu stressant pour le noyau grin

Je connais pas l'api win32 équivalente à pthread_join, qui attend qu'un thread soit terminé... ça aiderait, ptet, chais pas.