I want to create a thread or task (more than one to be exact) that goes and does some non CPU intensive work that will take a lot of time because of external causes, such a HTTP request or a file IO operation from a slow disk. I could do this with async await in C# and would be exactly what i am trying to do here. Spawn a thread or task and let it do it's own thing while i continue with execution of the program and simply let it return the result whenever ready. The problem with TBB i have is that all tasks i can make think they are created for a CPU intensive work.
Is what TBB calls GUI Thread what i want in this case ? I would need more than one, is that possible ? Can you point me to the right direction ? Should i look for another library that provides threading and is available for multiple OS ?
Any I/O blocking activity is poorly modeled by a task -- since tasks are meant to run to completion, it's just not what tasks are for. You will not find any TBB task-based approach that circumvents this. Since what you want is a thread, and you want it to work more-or-less nicely with other TBB code you already have, just use TBB's native thread class to solve the problem as you would with any other threading API. You won't need to set priority or anything else on this TBB-managed thread, because it'll get to its blocking call and then not take up any further time until the resource is available.
About the only thing I can think of specifically in TBB is that a task can be assigned a priority. But this isn't the same thing as a thread priority. TBB task priorities only dictate when a task will be selected from the ready pool, but like you said - once the task is running, it's expected to be working hard. The way to do use this to solve the problem you mentioned is to break your IO work into segments, then submit them into the work pool as a series of (dependent) low-priority tasks. But I don't think this gets to your real problem ...
The GUI Thread you mentioned is a pattern in the TBB patterns document that says how to offload a task and then wait for a callback to signal that it's complete. It's not altogether different from an async. I don't think this solves your problem either.
I think the best way for you here is to make an OS-level thread. That's pthreads on Linux or windows threads on Windows. Then you'll want to call this on it: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686277(v=vs.85).aspx ... if you happen to be in C++11, you could use a std::thread to create the thread and then call thread::native_handle to get a handle to call the Windows API to set the priority.
Related
I'm trying to create a task manager, which accepts tasks and runs each task as a new thread, using C++ and (currently) std::thread on a Linux environment .
the task manager accepts normal tasks and priority tasks.
when a priority task arrives, all normal tasks need to be halted until the priority task is done.
I'm keeping all normal task threads in a std::vector, but I couldn't find a proper function to halt those threads.
is there a way, preferably not using locks, to implement the wanted behavior?
maybe with <pthread> or boost threads?
There is no direct way to interrupt a thread from the outside.
Boost interruption points are handy to stop things once for all but that's not equivalent to a pause.
I would suggest you to implement your own "interruption" class with a condition variable (and yes a mutex) to check and wait efficiently anywhere inside your tasks. But it is up to you to explicitely call these interruptions.
Maybe another way would be make your priority tasks multithreadable so that you can allocate more threads to fulfill them => the scheduler is more likely to complete them first but that's not sure so forget what i said.
Sorry, I don't aknowledge anything better then this.
I'm creating a multi-threaded program which highly relays on boost::fibers when it comes to executing the actual execution heavy code.
What I want to achieve:
My main thread knows when to activate and run which fiber and when to collect the future provided after the execution of the fiber.
As my software is running in a highly parallel environment with over 100 cores, I start as many worker threads if possible. The worker threads should run a fiber scheduler like boost::fibers::algo::work_stealing. Therefore they should execute all the fibers my main thread produces.
What's the issue:
As my main thread is quite buisy with creating and timing all the fibers for the over 100 worker threads I want to avoid the main thread from joining the execution of any fibers.
This means, my main thread should solely care about starting fibers and gathering their futures after they completed executing.
However, I do not know how to exclude my main fiber from executing fibers, too.
Possible, naive, solutions:
Even though I don't know how to solve my problem properly, I thougt about a few possibilities.
Create my own fiber scheduler: The scheduler is a customization point of boost::fibers. Hence, there might be the possibility to wirte a custome scheduler which excludes the main tread from execution.
Use a boost::fibers::buffered_channel to transmit tasks between threads: However, I don't if this is a good solution as this removes the great possibilities from using a fiber scheduler.
Ohter great way I don't know yet: I gues there might be another, simple way from excluding the main thread, which creates the fibers, from participating in the execution scheduling of boost::fibers
As I'm quite new in the boost::fibers library, I wonder what is the best way to achieve my goal?
Regards and thank you for your help!
I will have a linux service that waits messages from a central and let the tasks do that are ordered by those messages. I think to do is I need to create a new thread.
Moreover, one task would have an absolute priority compared to others and when the order for that task comes, I need to accomplish it as soon as possible. Also, since all these stuff is on embedded system and resources are restricted, I thought I need to pause all other threads that were created.
I imagine that I need some thing similar as here:
How to sleep or pause a PThread in c on Linux
But the question is not duplicate. I do not have exact point to pause threads. I need to pause them wherever possible, and continue when the prioritized task is finished.
And here it suggests a way that seems obsolete and also I could use std::thread.
How to pause a pthread ANY TIME I want?
How could I achieve prioritize one task?
(Maybe before that) To do tasks, do I need to design some thing like "Thread manager", or there could be simpler thoughts?
Note: I have used the word "task" as it is, not as a technical term.
I want to split some CPU intensive jobs to multiple threads. I want to make a thread pool with, let's say, 4 threads.
I want to know very fast ways to do following:
Check if one thread is free for receiving processing
Signalize one thread to start specific function
Wait for all the threads to finish their jobs
This should be as fast as possible. I use C++ in Visual Studio 2010 on Windows 7. Any Win7/VS2010 specific solution would be preferred if it's faster than portable approach.
EDIT:
I found on MSDN this sample:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686946(v=vs.85).aspx
Is there any faster way to do this?
The stuff from the Boost thread library is pretty fast. You can start 4 threads that end up waiting for a boost::condition_variable. In the main thread you can add stuff to a task-queue and then call boost::condition_variable::notify_one in order to start one free thread, if any. As soon as one of the working threads is notified, it takes stuff out of the task queue and continues to do so until the queue is empty. In order to wait for the task queue to finish, let the thread that makes the task queue empty call boost::condition_variable::notify_all and wait in the main thread for that signal. Obviously you need to protect the shared data for this stuff with a mutex.
This technique works fine if you have medium to large size tasks and several thousand or less should execute in a second. I don't have experience with smaller tasks using this technique.
The parallel patterns library (PPL) is really good at that stuff too, it does a lot of stuff for you, but you don't have as much control. It's Windows only, but that seems to be fine with you. ;)
EDIT: Your link seems to be a good solution. Using the WINAPI is often the fastest thing you can do, since other APIs are usually build upon it. The WINAPI does not, however, provide very good abstraction. Thus I would prefer PPL, futures, etc. to perform tasks like that. How big are your tasks? If they take more than a few milliseconds, then you shouldn't worry about the api you're using, since that's not the bottleneck.
First way: Asynchronous Procedure Calls.
Another way: I/O Completion Ports, which can be used for your task.
I don't know about Visual C++ specific Thread Pools however I've heard about existance of some ppl.h. there an unofficial boost threadpool that I've use one. and just as all other boost It compiles well in Visual Studio
try tbb
class SimpleTask: public tbb::task {
public:
SimpleTask(const char *c ) {}
task* execute() {
//do task
return 0;
}
};
//execute tasks and wait
tbb::task_scheduler_init init(50);//initialize pool
tbb::task_list list;
for(int i=0;i<30;i++){//create 30 task
list.push_back(*new(tbb::task::allocate_root()) SimpleTask());
}
tbb::task::spawn_root_and_wait(list);//execute and wait for all task or call spawn without wait
I'm writing some computer vision software, here's a brief description to help clarify the problem:
I have 3 cameras, each running at 60fps
Each camera has it's own thread, to utilise multiple cores
Each thread waits for a new frame to arrive, does some processing on the image, saves the result and waits for the next frame
My main program creates these thread, using boost, following this tutorial: http://blog.emptycrate.com/node/282
I am currently polling the threads in a tight loop to retrieve the data, e.g.:
while(1) {
for(i=0; i<numCams; i++) {
result[i] = cam[i]->getResult();
}
//do some stuff
}
This seems silly. Is there a standard way of letting the main program know that there is a new result and that it needs to be retrieved?
Thanks!
Yes, you need to use condition variables (AKA events).
Yes, you need to use synchronization. There are many forms depending on what you're using as a threading API, however the simplest is probably a condition variable.
What you need is a thread pool. The number of cameras isn't necessary the same as the optimal number of threads. Thread pool is optimized for performance. Then, you don't need to wait for condition or poll the jobs, you enqueue the jobs (most often it's std::function<void()>) in the thread pool, and that job object should perform all the required work. Use binders (std::bind) or lambda functions to create a job object.
In your case you are talking to hardware, so you may need to use whatever facilities your camera API provides for asynchronous notification of incomming data. Usually that will be some kind of callback you provide, or occasionally something like a Windows Event handle or Unix signal.
In general if you meant "standard" as in "part of the C++ standard", no. You need to use your OS's facilites for interprocess (or thread) condition signalling.
Note that if we were talking Ada (or Modula-2, or many other modern systems programming languages) the answer would have been "yes". I understand there is some talk of putting concurrency support of some kind into a future C++ standard.
In the meantime, there is the boost::thread library for doing this kind of thing. That isn't exactly "standard", but for C++ it is pretty close. I think for what you are trying to do, condition variables might be what you want. However, if you read over the whole facility, other simpler designs may occur to you.
I know this sounds a little odd, however consider using a boost::asio::io_service it's as close to a threadpool as you get currently. When you've captured an image, you can post to this service and the service can then execute a handler asynchronously to handle your image data.