-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.cpp
151 lines (124 loc) · 4.02 KB
/
main.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
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
/*
* File: main.cpp
* Author: Piotr Gregor peter cf16 eu
*
* Created on May 22, 2013, 5:59 PM
*/
#include <DataAccessLayer/globals.h>
#include <DataAccessLayer/PosixClient.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <DataAccessLayer/ProcessMsgClass.h>
#include <DataAccessLayer/Repository.h>
#include <GUI/GUIMarketData.h>
#include <GUI/MainWindow.h>
#include <GUI/ReqMktDataGUI.h>
#include <GUI/ReqMktDepthGUI.h>
#include <boost/shared_ptr.hpp>
#include <pthread.h>
#include <errno.h>
// initialize global data
boost::shared_ptr<IB::PosixClient> client;
Repository marketDataRepository;
pthread_mutex_t repoMutexes[NUM_REPOTHREADS];
pthread_cond_t repoConditions[NUM_REPOTHREADS];
pthread_attr_t repoAttr;
/**
* initialize mutexes that protect Repository vectors
*/
void init_repoMutexes() {
for (int i = 0; i < NUM_REPOTHREADS; i++) {
pthread_mutex_init(&repoMutexes[i], NULL);
pthread_cond_init(&repoConditions[i], NULL);
}
}
void destroy_repoMutexes() {
for (int i = 0; i < NUM_REPOTHREADS; i++) {
pthread_mutex_destroy(&repoMutexes[i]);
pthread_cond_destroy(&repoConditions[i]);
}
}
// initialize static members
int ReqMktDataGUI::totalGUIReqActive = 0;
int ReqMktDepthGUI::totalGUIReqActive = 0;
#define NUM_THREADS 1
pthread_t thread[NUM_THREADS];
pthread_mutex_t continue_mutex; /* mutex used for processMessages as quit flag */
pthread_attr_t attr;
pthread_mutex_t client_mutex; /* mutex used for processMessages to avoid segmentation fault */
/* Returns 1 (true) if the mutex is unlocked, which is the
* thread's signal to terminate.
*/
int needQuit(pthread_mutex_t *mtx) {
switch (pthread_mutex_trylock(mtx)) {
case 0: /* if we got the lock, unlock and return 1 (true) */
pthread_mutex_unlock(mtx);
return 1;
case EBUSY: /* return 0 (false) if the mutex was locked */
return 0;
}
return 1;
}
/* Thread function containing a loop that's infinite except that it checks for
* termination with needQuit()
*/
void* processMessages(void* t) {
pthread_mutex_t *mx = (pthread_mutex_t *) t;
while (!needQuit(mx)) {
pthread_mutex_lock(&client_mutex); // avoid segmentation fault
client->processMessages();
pthread_mutex_unlock(&client_mutex);
}
//pthread_exit(NULL);
return NULL;
}
void processMessages() {
//pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
int rc;
pthread_mutex_init(&continue_mutex, NULL);
pthread_mutex_lock(&continue_mutex);
pthread_mutex_init(&client_mutex, NULL);
init_repoMutexes();
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
printf("global::processMessages: creating thread !\n");
rc = pthread_create(&thread[0], &attr, ::processMessages, &continue_mutex);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
void processMessages2() {
client->processMessages();
}
void endProcessMessages() {
/* unlock mxq to tell the processMessages thread to terminate, then join the thread */
pthread_mutex_unlock(&continue_mutex);
pthread_join(thread[0], NULL);
destroy_repoMutexes();
// pthread_attr_destroy(&attr);
// pthread_mutex_destroy(&mxq);
// pthread_mutex_destroy(&mxq2);
printf("global::endProcessMessages!\n");
}
void processMessages3() {
ProcessMsgClass pmc(client);
QThread t;
pmc.moveToThread(&t);
t.start();
sleep(10);
}
int main(int argc, char *argv[]) {
qRegisterMetaType<rec_ptr > ("rec_ptr");
// initialize resources
client.reset(new IB::PosixClient());
// Q_INIT_RESOURCE(resfile);
QApplication app(argc, argv);
app.setStyleSheet("QMenu::item:selected {border: 1px solid blue;}");
// create QMainWindow::QWidget and show it
cf16tradingclient_1 cf16(client, marketDataRepository);
cf16.show();
//pthread_exit (NULL);
return app.exec();
}