![]() Topic awaiting preservation: C Problem with Threads (Page 1 of 1) |
|
|---|---|
|
Paranoid (IV) Inmate From: Paris, France |
posted 09-14-2006 02:21
Hi, code: #include <windows.h>
#include <stdio.h>
#include <conio.h>
DWORD trace_test_err(LPCTSTR position, DWORD numero, BOOL trace, DWORD ignore_error, BOOL stop);
DWORD WINAPI thread_bad(HANDLE handle_main);
/*************************************************************************/
/* main function
/*************************************************************************/
int main (void)
{
/* initialisation des paramètres de l'unité d'exécution secondaire */
HANDLE handle_thread; /* handle sur l'objets du noyau thread */
SECURITY_ATTRIBUTES sa; /* attribut de sécurité */
DWORD dwStackSize; /* taille de la pile pour l'unité d'exécution */
LPTHREAD_START_ROUTINE lpStartAdress; /* pointeur sur la fonction unité d'exécution */
DWORD dwCreationFlags; /* flag de création pour l'unité d'exécution */
DWORD lpThreadId; /* identification de l'unité d'exécution */
lpStartAdress = thread_bad; /* fonction à lancer comme thread */
dwStackSize = 0; /* taille de la pile par défaut : 1mo */
dwCreationFlags = 0; /* création en mode normal */
sa.nLength = sizeof(SECURITY_ATTRIBUTES); /* taille de la structure */
sa.lpSecurityDescriptor = NULL; /* sécurité par défaut */
sa.bInheritHandle = FALSE; /* handle non héritable */
HANDLE handle_current_process = GetCurrentProcess(); /* récupération d'un pseudo handle pour le process en cours */
HANDLE handle_current_thread = GetCurrentThread(); /* récupération d'un pseudo handle pour la tâche en cours */
HANDLE handle_copy; /* handle qui recevra le handle dupliqué */
if ( ! (DuplicateHandle(handle_current_process, handle_current_thread, handle_current_process, &handle_copy, 0, FALSE, DUPLICATE_SAME_ACCESS)) )
trace_test_err("DuplicateThread", 0, TRUE, 0, TRUE);
if ( ! (handle_thread = CreateThread(&sa, dwStackSize, lpStartAdress, handle_copy, dwCreationFlags, &lpThreadId)))
trace_test_err("CreateThread", 0, TRUE, 0, TRUE);
// do some things
ExitThread(0);
}
/*************************************************************************/
/* sub function
/*************************************************************************/
DWORD WINAPI thread_bad(HANDLE handle_main)
{
// do some things
if ( ! TerminateThread(handle_main, 0) )
trace_test_err("TerminateThread", 0, TRUE, 0, TRUE);
// do some other things
ExitThread(0);
}
|
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 09-14-2006 10:46
I don't get your 'TerminateThread ( handle_main...)' - what's that supossed to do? |
|
Paranoid (IV) Inmate From: Paris, France |
posted 09-14-2006 14:58
I did not know what really were semaphores when I posted, but since you mentionned them I read some articles about the topic... And indeed using an event semaphore or a mutex semaphore along with WaitForSingleObject seems to be the way to go for the kind of things I had in mind. |