I want to be able to start a new thread from the main thread in my program. At the moment I'm using the following code:
std::thread acceptThread(Accept);
acceptThread.join();
But that blocks the continuation of the main thread until the acceptThread is "done". I don't want this. I just want it to run on a different thread so the main thread can continue. How do I do this?
What I want is a loop that accepts incoming connections and a loop that sends/receives data. These should be running at the same time.
The entire point of join() is to block until the other thread exits. Just get rid of it. Why'd you add it in the first place?
std::thread acceptThread(Accept);
creates and start a new thread that will call the "Accept" method, which is enough in your case.
Your second line is not needed, join would block the current thread unil the joined one exits.
see http://en.cppreference.com/w/cpp/thread/thread/join
Related
What's the best practice to achieve this :
1 - Thread for gathering data
2 - Wait for (1) to finish and render data
And those, indefinitely
while (true) {
thread tGatherData(getData); // Get data
tGatherData.join(); // Wait for data
thread tRender(render); // Render data
Sleep(3);
}
Using it like this doesn't sound like a good practice because it creates a new thread everytime right ?
How should I proceed ? Thanks
If you don't want to create thread on each iteration of your loop, you may want to start 2 threads: one for gathering information, second for printing it, and place your loop in both threads(you should remember about synchronization)
For example here I've created two threads: first for reading from console, second for printing. Synchronization is done by atomic. The program will stop execution, when it reads 0.
But, as good as I understand your code, you don't need second thread at all. All You need is to place your render function inside main thread. And your gathering function isn't asynchronous. It just create another thread and instantly start to wait for it's finishing, so your program should be single thread
I can't seem to find an exact answer to the threading question I have. I currently have a program that polls the Kinect V2 sensor for new frame data using OpenNI2. The problem is I need to poll each type of sensor; depth, IR, and RGB separately in order to get them at the same time. Here is where my threading questions comes in, I want to poll each of the three sensors in their own individual thread and when all functions calls have returned continue with the data processing.
I understand how to link each new thread to the main thread to ensure all threads finish before the program exits, but how do I wait in the middle of my program for a specific set of threads to finish? Is there a function or feature of std::thread in c++11 that I am overlooking or is this something that needs to be manually done using a mutex or semaphores?
pseudocode:
std::thread thread_RGB(Kinect::readFrame(Enum::RGB), kinect_1);
std::thread thread_IR(Kinect::readFrame(Enum::IR), kinect_1);
std::thread thread_depth(Kinect::readFrame(Enum::depth), kinect_1);
// Wait for all threads to finish getting new frame data
...
// Process data here
process_data(kinect_1.RGB_data);
process_data(kinect_1.IR_data);
process_data(kinect_1.depth_data);
You need to call .join method to wait for the threads to finish and then destruct them. When you call t.join() there is a check if the thread is still doing something and if the work is done the thread is joined. If the thread is not joinable(there is also t.joinable()) the main thread will wait till the secondary thread finish its work and then join it.
In your case you can add these lines.
thread_RGB.join();
thread_IR.join();
thread_depth.join();
Here is an image I found on google that shows how thread::join() works.
I need to create, run, stop thread and then again same process (reloading some new data and need to refresh and cannot use C++11 standard). I have created and run thread like from mine main thread
pthread_t p;
pthread_create(&p, NULL, calculation, some_pointer_to_object);
How to stop and destroy this thread from main thread ?
(pthread_exit is from current thread).
You need to use pthread_cancel().
The only clean way to do so is this: Set up a flag in the main thread, start the thread, poll the flag in your new thread and finish fast if it's set. Everything else but letting your new thread close itself down cleanly on request opens a boatload of cans of worms, and that's an understatement.
In my program, it start a boost thread and keep the handler as a member of the main thread.
When user press the cancel button I need to check the started thread still running and if it is running need tho kill that specific thread. here is the pseudo code.
cheating thread
int i =1;
boost::thread m_uploadThread = boost::thread(uploadFileThread,i);
This is the method use to check if thread is still running, but it is not working
boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(2);
if (this->uploadThread.timed_join(timeout)){
//Here it should kill the thread
}
The return value true means the thread is completed before the call times out. And looks like what you want is
if(!this->uploadThread.timed_join(timeout))
For stop your thread you can use:
my_thread.interrupt();
in order this to work you have to set an interruption point at the point you want the thread's function stops when you interrupt.
Note: the interruption by it self don't stop the thread it just set a flag and the when an interruption point is reached the thread is interrupted. If no interruption point is found, the thread don't stop.
You can also handle the interrupted exception boost::thread_interrupted that way you can do things depending on if the thread was interrupted or not.
For instance lets assume the next code is inside a thread function:
try
{
//... some important code here
boost::this_thread.interruption_poit(); // Setting interrutption point.
}
catch(boost::thread_interrupted&)
{
// Now you do what ever you want to do when
// the thread is interrupted.
}
My application has to suspend and resume a different process every few *microsec*s.
It works fine only sometimes it feels like it suspends the process for non-uniforms times.
I use the win API: ResumeThread and SuspendThread.
On the other hand, I tried something a little different.
I suspended the thread as usual with SuspendThread but when I resume it, I do like so:
while (ResumeThread(threadHandle) > 0);
and it works faster and it runs the other process in a uniform pace.
Why does it happen? is it possible that sometimes the thread gets suspended twice and then the ResumeThread command executes?
thanks :)
SuspendThread() call does not suspend a thread instantly. It takes several time to save an execution context, so that ResumeThread() might be called when a thread has not suspended yet. That's why while (ResumeThread(threadHandle) > 0); works. To determine the current thread state you can call NtQueryInformationThread(), but only in NT versions of Windows.
If you have a loop in the secondary thread, you can change your synchronization with a Manual Reset Event. The primary thread should call ResetEvent() to suspend a thread, and SetEvent() to resume. The secondary thread every loop should call WaitForSingleObjectEx().
I followed Damon's suggestion, removed suspend / resume from the code and instead used a synchronization object over which my pool thread waits infinitely after completing the work and the same is signaled by my server thread after allocating work to it.
The only time I have to use suspend now is when I create the thread for the first time and after allocating work to it the thread is resumed from my server thread. The thread created is used in a thread pool over and over again to do some work.
It is working like a charm.
Thank you Damon!
Regards,
Ajay
thats the way de loop look's like
for(i=0;i<num;i++) {
while (ResumeThread(threadHandle) > 0);
ResumeThread(threadHandle)
SuspendThread(threadHandle);
}
SuspendThread takes a few milliseconds so the while loop goes on until thread is suspended, after that, again the thread process SuspendThread function is called, a good way to call GetProcessContext to see EIP