-2

I work with Windows 10 with codeblocks and gcc 7.2 and I can use c++11 or c++14. If possible without external libraries (boost). I have a program that process hundreds of millions of fields.It´s a concept proof so I don´t need fancy error handling. I need simplicity and performance. The fields are mapped to a buffer of bits, that can reach more than 500MB, and I would like to put the management of the buffer in a isolated thread. The constructor or initialization would do the creation of a vector, the destructor or end function would write the buffer to disk with statistics and the function that will be called millions of times only make some calculus and set the bits. I need to call that function asynchronous from the main program and I don´t need return of any data. Just to set the bits and if there are erros I can read than when the processing ends if it is complex to do otherwise. The main program can be replicated in 2 or 4 threads that access the same function. I appreciate guidance for the asynchronous call of that function. Thank you.

  • Well C++11 and later supports [std::thread](http://en.cppreference.com/w/cpp/thread/thread) – Chris O Dec 17 '17 at 23:34
  • I used it and it works ok. How can I call asynchronous in a way tat uses the thread I want without futures because I don't want to block the main program and I don´t need answer back to the main? – Ribeiro Silva Dec 17 '17 at 23:44
  • You can refer to this answer: https://stackoverflow.com/questions/10890242/get-the-status-of-a-stdfuture –  Dec 17 '17 at 23:48
  • [Like in the example](http://en.cppreference.com/w/cpp/thread/thread/thread) your `main()` starts whatever n threads, but if you won't want to block the main thread, then don't call `join()`. Also `main()` must stay alive long enough, so your threads don't get terminated --> I *think* that's correct, if `main()` ends, then your program also ends, whether or not your threads are still running (a quick test can verify this). – Chris O Dec 17 '17 at 23:55
  • Yes, that's correct, I got an exception when `main()` exited too early and the threads were still running (because I removed the `join()`). So if you don't want to block `main()` then at least move the `joins` to the very end of `main()` for correctness, and let `main()` do whatever else it wants before that. – Chris O Dec 18 '17 at 00:04
  • I'm not sure what the question is. Are you just unaware of `std::thread::detach()` ? – MSalters Dec 18 '17 at 10:24
  • Now my monolithic program with a little test data of 1 million fields to process takes less than 1 second. It executes a little part of the code 120000 times. I want to isolate that part of the code without compromising the speed. Do you think it is possible to do inside an independent thread? A test with launch::async takes 6 seconds for 100000 executions without code, maybe creating 100000 threads is bad. – Ribeiro Silva Dec 19 '17 at 10:43
  • Writer with Mutex lock/ push queue/ unlock/ cond variable notify and reader with mutex lock/ con variable wait/ queue.front/unlock is too slow. I am processing more than 20 million fields per second. My problem is not with the join neither with the detach. And I used pthreads before with semaphores but now I would like to use C++ standard. As fast as possible. – Ribeiro Silva Jan 03 '18 at 13:01

1 Answers1

-1

Sorry for my english. The question is about communicating with a thread millions of times without blocking the main program and without spending cpu when there is no work to do by the thread. And with good performance because the program code is very short but it is runned a lot of times.