@@ -157,92 +157,93 @@ int main(int argc, char** argv) {
157157
158158 if (count == 0 ) throw std::runtime_error (" No files given.\n " );
159159
160-
161160#ifdef _WIN32
162161 // Threading doesn't work in windows yet.
163- for (int i=0 ;i<count;i++) {
164- codegen_response_t * response = codegen_file ((char *)files[i].c_str (), start_offset, duration, i);
165- char *output = make_json_string (response, human_readable_code
166- );
167- print_json_to_screen (output, count, i+1 );
168- if (response->codegen ) {
169- delete response->codegen ;
170- }
171- free (response);
172- free (output);
173- }
174- return 0 ;
175-
162+ bool use_threads = false ;
176163#else
164+ bool use_threads = (count > 1 );
165+ #endif
177166
178- // Figure out how many threads to use based on # of cores
179- int num_threads = getNumCores ();
180- if (num_threads > 8 ) num_threads = 8 ;
181- if (num_threads < 2 ) num_threads = 2 ;
182- if (num_threads > count) num_threads = count;
183-
184- // Setup threading
185- pthread_t *t = (pthread_t *)malloc (sizeof (pthread_t )*num_threads);
186- thread_parm_t **parm = (thread_parm_t **)malloc (sizeof (thread_parm_t *)*num_threads);
187- pthread_attr_t *attr = (pthread_attr_t *)malloc (sizeof (pthread_attr_t )*num_threads);
188-
189- // Kick off the first N threads
190- int still_left = count-1 -already;
191- for (int i=0 ;i<num_threads;i++) {
192- parm[i] = (thread_parm_t *)malloc (sizeof (thread_parm_t ));
193- parm[i]->filename = (char *)files[still_left].c_str ();
194- parm[i]->start_offset = start_offset;
195- parm[i]->tag = still_left;
196- parm[i]->duration = duration;
197- parm[i]->done = 0 ;
198- still_left--;
199- pthread_attr_init (&attr[i]);
200- pthread_attr_setdetachstate (&attr[i], PTHREAD_CREATE_DETACHED);
201- // Kick off the thread
202- if (pthread_create (&t[i], &attr[i], threaded_codegen_file, (void *)parm[i]))
203- throw std::runtime_error (" Problem creating thread\n " );
204- }
205-
206- int done = 0 ;
207- // Now wait for the threads to come back, and also kick off new ones
208- while (done<count) {
209- // Check which threads are done
167+ if (!use_threads) {
168+ for (int i=0 ;i<count;i++) {
169+ codegen_response_t * response = codegen_file ((char *)files[i].c_str (), start_offset, duration, i);
170+ char *output = make_json_string (response, human_readable_code);
171+ print_json_to_screen (output, count, i+1 );
172+ if (response->codegen ) {
173+ delete response->codegen ;
174+ }
175+ free (response);
176+ free (output);
177+ }
178+ return 0 ;
179+ } else {
180+ // Figure out how many threads to use based on # of cores
181+ int num_threads = getNumCores ();
182+ if (num_threads > 8 ) num_threads = 8 ;
183+ if (num_threads < 2 ) num_threads = 2 ;
184+ if (num_threads > count) num_threads = count;
185+
186+ // Setup threading
187+ pthread_t *t = (pthread_t *)malloc (sizeof (pthread_t )*num_threads);
188+ thread_parm_t **parm = (thread_parm_t **)malloc (sizeof (thread_parm_t *)*num_threads);
189+ pthread_attr_t *attr = (pthread_attr_t *)malloc (sizeof (pthread_attr_t )*num_threads);
190+
191+ // Kick off the first N threads
192+ int still_left = count-1 -already;
210193 for (int i=0 ;i<num_threads;i++) {
211- if (parm[i]->done ) {
212- parm[i]->done = 0 ;
213- done++;
214- codegen_response_t *response = (codegen_response_t *)parm[i]->response ;
215- char *json = make_json_string (response, human_readable_code);
216- print_json_to_screen (json, count, done);
217- if (response->codegen ) {
218- delete response->codegen ;
219- }
220- free (parm[i]->response );
221- free (json);
222- // More to do? Start a new one on this just finished thread
223- if (still_left >= 0 ) {
224- parm[i]->tag = still_left;
225- parm[i]->filename = (char *)files[still_left].c_str ();
226- still_left--;
227- int err= pthread_create (&t[i], &attr[i], threaded_codegen_file, (void *)parm[i]);
228- if (err)
229- throw std::runtime_error (" Problem creating thread\n " );
194+ parm[i] = (thread_parm_t *)malloc (sizeof (thread_parm_t ));
195+ parm[i]->filename = (char *)files[still_left].c_str ();
196+ parm[i]->start_offset = start_offset;
197+ parm[i]->tag = still_left;
198+ parm[i]->duration = duration;
199+ parm[i]->done = 0 ;
200+ still_left--;
201+ pthread_attr_init (&attr[i]);
202+ pthread_attr_setdetachstate (&attr[i], PTHREAD_CREATE_DETACHED);
203+ // Kick off the thread
204+ if (pthread_create (&t[i], &attr[i], threaded_codegen_file, (void *)parm[i]))
205+ throw std::runtime_error (" Problem creating thread\n " );
206+ }
230207
208+ int done = 0 ;
209+ // Now wait for the threads to come back, and also kick off new ones
210+ while (done<count) {
211+ // Check which threads are done
212+ for (int i=0 ;i<num_threads;i++) {
213+ if (parm[i]->done ) {
214+ parm[i]->done = 0 ;
215+ done++;
216+ codegen_response_t *response = (codegen_response_t *)parm[i]->response ;
217+ char *json = make_json_string (response,human_readable_code);
218+ print_json_to_screen (json, count, done);
219+ if (response->codegen ) {
220+ delete response->codegen ;
221+ }
222+ free (parm[i]->response );
223+ free (json);
224+ // More to do? Start a new one on this just finished thread
225+ if (still_left >= 0 ) {
226+ parm[i]->tag = still_left;
227+ parm[i]->filename = (char *)files[still_left].c_str ();
228+ still_left--;
229+ int err= pthread_create (&t[i], &attr[i], threaded_codegen_file, (void *)parm[i]);
230+ if (err)
231+ throw std::runtime_error (" Problem creating thread\n " );
232+
233+ }
231234 }
232235 }
233236 }
234- }
235237
236- // Clean up threads
237- for (int i=0 ;i<num_threads;i++) {
238- free (parm[i]);
238+ // Clean up threads
239+ for (int i=0 ;i<num_threads;i++) {
240+ free (parm[i]);
241+ }
242+ free (t);
243+ free (parm);
244+ free (attr);
245+ return 0 ;
239246 }
240- free (t);
241- free (parm);
242- free (attr);
243- return 0 ;
244-
245- #endif // _WIN32
246247 }
247248 catch (std::runtime_error& ex) {
248249 fprintf (stderr, " %s\n " , ex.what ());
0 commit comments