![]() |
Multithreading and locking?
According to the documentation, "Task.h" provides a way to do multithreading on the robot. I wanted to try running my camera processing code on a separate thread and ping back on it periodically to grab the latest set of data it produces.
I'm pretty new to multithreading, but as best as I can tell, I need to lock a variable or something before I read/write to it to make sure things don't go haywire. However, I don't know how to lock a variable or otherwise communicate between a thread and the main program. How would I accomplish this? If possible, could somebody point to some example code that does something similar so I can use it for reference? |
Re: Multithreading and locking?
Google around for "VxWorks semaphore", and have a look at the SEM_ID data structure.
|
Re: Multithreading and locking?
Just use global variables. Don't tell anyone (I know it's bad), but our team does't use any lock/semaphore-type structures, and we've never had a problem. Then again, the data shared between the threads is only a float and a string.
|
Re: Multithreading and locking?
Quote:
|
Re: Multithreading and locking?
One example worth looking at is the PIDController. It spawns a task which updates every 50ms from the PIDInput and writes its results to PIDWrite. That will serve as a good example. But if there's a lack of clarity as to what you're really trying to do, here's "it" in a nutshell. Since you have two tasks running at the same time, one must be careful to not wind up modifying something while it is being read by the other task. The lock basically says that the other task cannot get their lock until you unlock on the other one. One must also be careful to program in a manner where you always give up a lock that you've taken - or else the other thread will be forever waiting to gain the lock which you never give up.... and similarly, if you take a lock, you have to be careful to make sure you don't accidentally call a function which tries to take the lock a 2nd time which would result in the same deadlock situation.
bob |
Re: Multithreading and locking?
Quote:
The AxisCamera code also has some locking that you can look at. We also wrapped it a little in our code: https://github.com/rbmj/612-code/blo...ion_thread.cpp |
Re: Multithreading and locking?
Quote:
so for example right after your includes you would put something like this... using namespace cv; using namespace std; Now that you have that instead of doing something like std::cout << cv::Point(10,10) << std::endl; You can now do this cout << Point(10,10) << endl; Just something I thought I would share with everyone. |
Re: Multithreading and locking?
Quote:
|
Re: Multithreading and locking?
Quote:
|
Re: Multithreading and locking?
Quote:
|
Quote:
|
Quote:
|
Re: Multithreading and locking?
Quote:
|
Re: Multithreading and locking?
Quote:
|
| All times are GMT -5. The time now is 10:49 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi