slt,quand j 'ai un error ou warning comme ca:_beginthreadex' undefined; assuming extern returning int,
alors qu`est ce cela sighnifie et comment je peut le resoudre?
Version imprimable
slt,quand j 'ai un error ou warning comme ca:_beginthreadex' undefined; assuming extern returning int,
alors qu`est ce cela sighnifie et comment je peut le resoudre?
Cela signifie que le source qui utilise _beginthreadex n'a pas inclus le fichier include qui déclare _beginthreadex.
Comme il ne connait pas le prototype de la fonction, il assume que cette fonction retourne un entier de type int et il n'y a pas de contrôle sur les types de paramètres.
Il faudrait que tu ajoutes
pour que cela rentre dans l'ordreCode:#include <process.h>
mais il est deja inclu ledes le debut et voila le code en details:Code:#include <process.h>
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 /*** ThreadSync1.c ***/ #include <stdio.h> #include <conio.h> #include <process.h> #include <windows.h> #pragma pack(1) /*================== Constants =================================*/ #define BUFFER_SIZE (64*1023) #define NUM_OF_FUNCTIONS 2 #define THREAD_TOTAL_LIMIT 5 /*================= Data Types =================================*/ typedef struct _THREAD_PARAMETERS_ { char *szFileName; unsigned int uResult; } THREAD_PARAMETERS; typedef struct _THREAD_INFO_ { DWORD dwThreadID; void* pParam; unsigned int uFunction; } THREAD_INFO; typedef struct _THREAD_FUNCTIONS_ { DWORD(WINAPI *pThreadFunction[NUM_OF_FUNCTIONS])(void*); void(*pPrintFunction[NUM_OF_FUNCTIONS])(THREAD_INFO*); void*(*pInitParamFunction[NUM_OF_FUNCTIONS])(void); char* cpMenuItems[NUM_OF_FUNCTIONS]; unsigned int uNumOfThreads[NUM_OF_FUNCTIONS]; unsigned int uThreadLimit[NUM_OF_FUNCTIONS]; unsigned int uTotalThreads; THREAD_INFO* spThreadData[THREAD_TOTAL_LIMIT]; HANDLE hpThreads[THREAD_TOTAL_LIMIT]; } THREAD_FUNCTIONS; /*================== Function prototypes =======================*/ DWORD WINAPI CountVowels (void* pParam); void PrintNumOfVowels(void* pParam); void* InitCountVowels(void); DWORD WINAPI CountWords(void* pParam); void PrintNumOfWords(void* pParam); void* InitCountWords(void); /*================== Functions =================================*/ void InitializeThreadFunctions(THREAD_FUNCTIONS* spTF) { memset(spTF, 0, sizeof(THREAD_FUNCTIONS)); spTF->uTotalThreads = 0; spTF->cpMenuItems[0] = "Count the number of vowels in a text."; spTF->pThreadFunction[0] = CountVowels; spTF->pPrintFunction[0] = PrintNumOfVowels; spTF->pInitParamFunction[0] = InitCountVowels; spTF->uNumOfThreads[0] = 0; spTF->uThreadLimit[0] = 3; spTF->cpMenuItems[1] = "Count the number of words in a text."; spTF->pThreadFunction[1] = CountWords; spTF->pPrintFunction[1] = PrintNumOfWords; spTF->pInitParamFunction[1] = InitCountWords; spTF->uNumOfThreads[1] = 0; spTF->uThreadLimit[1] = 3; } /* InitializeThreadFunctions() */ /*--------------------------------------------------------------*/ void* InitCountVowels(void) { THREAD_PARAMETERS *spParam; spParam = (THREAD_PARAMETERS*)malloc(sizeof(THREAD_PARAMETERS)); if (spParam != NULL) { spParam->szFileName = "file1.txt"; spParam->uResult = 0; } return spParam; } /* InitCountVowels() */ /*--------------------------------------------------------------*/ DWORD WINAPI CountVowels (void* pParam) { /* Counts the number of vowels in a text. */ size_t i; FILE *fp; char *szText; THREAD_PARAMETERS *spData = (THREAD_PARAMETERS*)pParam; spData->uResult = 0; szText = malloc(BUFFER_SIZE * sizeof(*szText)); if (szText != NULL) { fp = fopen(spData->szFileName, "rb"); if (fp != NULL) { Sleep(3000); i = fread(szText, sizeof(*szText), BUFFER_SIZE, fp); fclose(fp); while (i != 0) { switch (toupper(szText[i])) { case 'A': case 'O': case 'U': case 'E': case 'I': case 'Y': spData->uResult++; } i--; } } // if (fp != NULL) free(szText); } // if (cpText != NULL) return 0; } /* CountVowels() */ /*--------------------------------------------------------------*/ void PrintNumOfVowels(THREAD_INFO* spThread) { THREAD_PARAMETERS* spParam = (THREAD_PARAMETERS*)(spThread->pParam); printf("Thread function %u (TID = 0x%X) completed. There are %u vowels in the text.\n", spThread->uFunction+1, spThread->dwThreadID, spParam->uResult); } /* PrintNumOfVowels() */ /*--------------------------------------------------------------*/ void* InitCountWords(void) { THREAD_PARAMETERS *spParam; spParam = (THREAD_PARAMETERS*)malloc(sizeof(THREAD_PARAMETERS)); if (spParam != NULL) { spParam->szFileName = "file2.txt"; spParam->uResult = 0; } return spParam; } /* InitCountWords() */ /*--------------------------------------------------------------*/ DWORD WINAPI CountWords(void* pParam) {/* Counts the number of words in a text. */ size_t i; FILE *fp; char *szText; THREAD_PARAMETERS *spData = (THREAD_PARAMETERS*)pParam; spData->uResult = 0; szText = malloc(BUFFER_SIZE * sizeof(*szText)); if (szText != NULL) { fp = fopen(spData->szFileName, "rb"); if (fp != NULL) { Sleep(4000); i = fread(szText, sizeof(*szText), BUFFER_SIZE, fp); fclose(fp); while (i != 0) { while (i != 0 && szText[i] == ' ') i--; if (i != 0) spData->uResult++; while (i != 0 && szText[i] != ' ') i--; } } // if (fp != NULL) free(szText); } // if (cpText != NULL) return 0; } /* CountWords() */ /*--------------------------------------------------------------*/ void PrintNumOfWords(THREAD_INFO* spThread) { THREAD_PARAMETERS* spParam = (THREAD_PARAMETERS*)(spThread->pParam); printf("Thread function %u (TID = 0x%X) completed. There are %u words in the text.\n", spThread->uFunction+1, spThread->dwThreadID, spParam->uResult); } /* PrintNumOfWords() */ /*--------------------------------------------------------------*/ THREAD_INFO* StartNewThread(THREAD_FUNCTIONS* spTF, unsigned int uFunction) { THREAD_INFO *spNewThread = NULL; if (uFunction < NUM_OF_FUNCTIONS) { if ( (spTF->uNumOfThreads[uFunction] < spTF->uThreadLimit[uFunction]) && (spTF->uTotalThreads < THREAD_TOTAL_LIMIT) ) { spNewThread = (THREAD_INFO*)malloc(sizeof(THREAD_INFO)); if (spNewThread != NULL) { spTF->spThreadData[spTF->uTotalThreads] = spNewThread; spNewThread->uFunction = uFunction; spNewThread->pParam = spTF->pInitParamFunction[uFunction](); spTF->hpThreads[spTF->uTotalThreads] = (HANDLE)_beginthreadex(NULL, 0, spTF->pThreadFunction[uFunction], spNewThread->pParam, 0, &(spNewThread->dwThreadID)); spTF->uTotalThreads++; spTF->uNumOfThreads[uFunction]++; } /* if (spNewThread != NULL) */ } } /* if (uFunction < NUM_OF_FUNCTIONS) */ return spNewThread; } /* StartNewThread() */ /*--------------------------------------------------------------*/ void PrintResults(THREAD_FUNCTIONS* spTF) { int i, j; DWORD dwExitCode; THREAD_INFO** spThreads = spTF->spThreadData; i = spTF->uTotalThreads - 1; while (i >= 0) { GetExitCodeThread(spTF->hpThreads[i], &dwExitCode); if (dwExitCode != STILL_ACTIVE) { printf(" PrintResults(): i=%d, uTotalThreads=%d.\n", i, spTF->uTotalThreads); spTF->pPrintFunction[spThreads[i]->uFunction](spThreads[i]); free(spThreads[i]->pParam); spThreads[i]->pParam = NULL; spTF->uTotalThreads--; spTF->uNumOfThreads[spThreads[i]->uFunction]--; free(spThreads[i]); spThreads[i] = NULL; CloseHandle(spTF->hpThreads[i]); for (j = i; j < (int)spTF->uTotalThreads; j++) { printf(" PrintResults(): j=%d, uTotalThreads=%d.\n", j, spTF->uTotalThreads); spThreads[j] = spThreads[j+1]; spTF->hpThreads[j] = spTF->hpThreads[j+1]; } spThreads[spTF->uTotalThreads] = NULL; spTF->hpThreads[spTF->uTotalThreads] = NULL; } /* if (dwExitCode != STILL_ACTIVE) */ i--; } /* for (i) */ } /* PrintResults() */ /*--------------------------------------------------------------*/ void DisplayMainMenu(const char** cpMenuItems, unsigned int uNumOfFunctions) { unsigned int i; printf("Choose one of the following actions:\n"); for (i = 0; i < uNumOfFunctions; i++) printf(" %u - %s\n", i+1, cpMenuItems[i]); printf(" q - Exit.\n"); } /* DisplayMainMenu() */ /*--------------------------------------------------------------*/ void main() { int nKey = 0; /* character entered from keyboard */ THREAD_FUNCTIONS sTF; THREAD_INFO* spThread; DWORD dwResult; DWORD dwTimeout = 10; BOOL bQuit; InitializeThreadFunctions(&sTF); DisplayMainMenu(sTF.cpMenuItems, NUM_OF_FUNCTIONS); do { if ( _kbhit() ) /* if a key has been pressed */ { nKey = _getch(); /* read the key code */ if (nKey >= '1' && nKey <= '2') { spThread = StartNewThread(&sTF, nKey-'1'); if (spThread != NULL) printf("Started thread function %u (TID = 0x%X).\n", spThread->uFunction+1, spThread->dwThreadID); else printf("Error: thread function %u cannot be started.\n", nKey-'0'); } } /* if ( _kbhit() ) */ bQuit = ((nKey == 'q') || (nKey == 'Q')); if (bQuit) dwTimeout = INFINITE; dwResult = WaitForMultipleObjects(sTF.uTotalThreads, sTF.hpThreads, bQuit, dwTimeout); if ( (dwResult != WAIT_FAILED) && (dwResult != WAIT_TIMEOUT) ) { printf("---main(): bQuit=%d, dwTimeout=%d, dwResult=%d, uTotalThreads=%d.\n", bQuit, dwTimeout, dwResult, sTF.uTotalThreads); PrintResults(&sTF); } Sleep(10); } while (!bQuit); printf("\nAll threads terminated.\n"); } /* main() */
Regarde dans ce thread http://www.developpez.net/forums/d24...d-identifiers/ (c'est pas tout récent), il faut que ton projet ait une option multi thread