Skip to content

Commit 33b5a42

Browse files
committed
fix integration test: add windows thread handling
1 parent 237b5a7 commit 33b5a42

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test/main.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
#include <stdlib.h>
1111
#include <utils.h>
1212
#include "sqlite3.h"
13+
#ifdef _WIN32
14+
#include <windows.h>
15+
#else
1316
#include <pthread.h>
17+
#endif
1418

1519
#define PEERS 5
1620
#define DB_PATH "health-track.sqlite"
@@ -263,7 +267,11 @@ int test_report(const char *description, int rc){
263267
return rc;
264268
}
265269

270+
#ifdef _WIN32
271+
DWORD WINAPI worker(LPVOID arg) {
272+
#else
266273
void* worker(void* arg) {
274+
#endif
267275
int thread_id = *(int*)arg;
268276

269277
char description[32];
@@ -295,20 +303,39 @@ int main (void) {
295303

296304
remove(DB_PATH); // remove the database file
297305

306+
#ifdef _WIN32
307+
HANDLE threads[PEERS];
308+
#else
298309
pthread_t threads[PEERS];
310+
#endif
299311
int thread_ids[PEERS];
300312

301313
for (int i = 0; i < PEERS; i++) {
302314
thread_ids[i] = i;
315+
#ifdef _WIN32
316+
threads[i] = CreateThread(NULL, 0, worker, &thread_ids[i], 0, NULL);
317+
if (threads[i] == NULL) {
318+
fprintf(stderr, "CreateThread failed\n");
319+
return 1;
320+
}
321+
#else
303322
if (pthread_create(&threads[i], NULL, worker, &thread_ids[i]) != 0) {
304323
perror("pthread_create");
305324
exit(1);
306325
}
326+
#endif
307327
}
308328

309329
// Wait for all threads to finish
330+
#ifdef _WIN32
331+
WaitForMultipleObjects(PEERS, threads, TRUE, INFINITE);
332+
#endif
310333
for (int i = 0; i < PEERS; i++) {
334+
#ifdef _WIN32
335+
CloseHandle(threads[i]);
336+
#else
311337
pthread_join(threads[i], NULL);
338+
#endif
312339
}
313340

314341
printf("\n");

0 commit comments

Comments
 (0)