-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_threads.cpp
75 lines (69 loc) · 1.81 KB
/
proxy_threads.cpp
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
#include "proxy_threads.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
HANDLE threads[NUM_THREADS];
#else
#include <unistd.h>
pthread_t threads[NUM_THREADS];
#endif
int thread_args[NUM_THREADS];
int completed_threads[NUM_THREADS];
int thread_ids[NUM_THREADS];
int get_next_thread_id()
{
for(;;)
{
for(int i=0; i<NUM_THREADS; i++)
{
if(completed_threads[i] == -1)
{
completed_threads[i] = 0;
return i;
}
if(completed_threads[i] == 1)
{
#ifdef _MSC_VER
CloseHandle(threads[i]);
#else
pthread_join(threads[i], NULL);
#endif
completed_threads[i] = 0;
return i;
}
}
printf("!!!!!!!!! All threads are busy - cannot accept new connection!!!!!!!\n");
sleep(10);
}
}
void new_thread(void * routine, int thread_arg)
{
int new_thread_id = get_next_thread_id();
thread_args[new_thread_id] = thread_arg;
#ifdef _MSC_VER
threads[new_thread_id] = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)routine,
(void*)(thread_ids + new_thread_id),
0,
NULL);
#else
int rc = pthread_create(&threads[new_thread_id],
NULL,
(void*(*)(void*))routine,
(void*)(thread_ids+new_thread_id));
if(rc)
{
printf("ERROR CREATING GAME THREAD: %d\n", rc);
exit(1);
}
#endif
}
void init_threads()
{
for(int i=0; i<NUM_THREADS; i++)
{
completed_threads[i] = -1;
thread_ids[i] = i;
}
}