Go to Post So Love your robot, and it will love you. - Not2B [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 21-04-2012, 19:55
Michael_Lee Michael_Lee is offline
Registered User
FRC #2976
 
Join Date: Jan 2012
Location: Issaquah, WA
Posts: 21
Michael_Lee is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 21-04-2012, 21:25
connor.worley's Avatar
connor.worley connor.worley is offline
Registered User
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Mar 2011
Rookie Year: 2010
Location: Berkeley/San Diego
Posts: 595
connor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond reputeconnor.worley has a reputation beyond repute
Re: Multithreading and locking?

Google around for "VxWorks semaphore", and have a look at the SEM_ID data structure.
__________________
Team 973 (2016-???)
Team 5499 (2015-2016)
Team 254 (2014-2015)

Team 1538 (2011-2014)
2014 Driver (25W 17L 1T)
日本語でOK
Reply With Quote
  #3   Spotlight this post!  
Unread 21-04-2012, 22:06
Skinkworks Skinkworks is offline
The guy with the Fez
FRC #1302 (Team Lionheart)
Team Role: Programmer
 
Join Date: Apr 2012
Rookie Year: 2008
Location: Kernel Space, NJ
Posts: 17
Skinkworks is on a distinguished road
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.
__________________
Yes, I am the same person from the FTC forum.
FTC #248 (2009-2013): Fatal Error, Programmer, Driver, and Builder.
FRC #3142 (2009-2012): Aperture, Build, Design, and Programming Leaders.
FRC #1302 (2012-2013): Lionheart, Build Captain.
ZRHS #89 (2011-2013): Team Kühlschrank, strategist and programmer.
ZRAC #40 (2012): Catcher in the Skye, programmer.
FLL #3149 #15193 (2011-2013): Mentor.
ISR 12: Umptysquatch 6, Designer, and Builder.
FRC #???? (2013-????) Mentor.
FTC #???? (2013-????) Mentor.
FLL #????? (2013-?????) Mentor.
--
11 seasons of FIRST in 6 years. I wish I could've done more.
Reply With Quote
  #4   Spotlight this post!  
Unread 21-04-2012, 22:14
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Multithreading and locking?

Quote:
Originally Posted by Skinkworks View Post
our team does't use any lock/semaphore-type structures, and we've never had a problem
Zera's Law: Once you realize that something bad can happen, it will.


Reply With Quote
  #5   Spotlight this post!  
Unread 22-04-2012, 02:56
bob.wolff68's Avatar
bob.wolff68 bob.wolff68 is offline
Da' Mentor Man
FRC #1967
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2007
Location: United States
Posts: 151
bob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nicebob.wolff68 is just really nice
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
__________________
~~~~~~~~~~~~~~~~~~~
Bob Wolff - Software from the old-school
Mentor / C / C++ guy
Team 1967 - The Janksters - San Jose, CA
Reply With Quote
  #6   Spotlight this post!  
Unread 22-04-2012, 12:03
rbmj rbmj is offline
Registered User
FRC #0612 (Chantilly Robotics)
Team Role: Alumni
 
Join Date: Apr 2011
Rookie Year: 2011
Location: DC Area/Fairfax County
Posts: 192
rbmj is a jewel in the roughrbmj is a jewel in the roughrbmj is a jewel in the rough
Re: Multithreading and locking?

Quote:
Originally Posted by Ether View Post
Zera's Law: Once you realize that something bad can happen, it will.


I can attest to that. As soon as I took a second look at our code I realized that we'd had static initialization order issues, and sure enough, a few days later it segfaulted...

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
Reply With Quote
  #7   Spotlight this post!  
Unread 13-09-2012, 19:32
jacob9706 jacob9706 is offline
Registered User
AKA: Jacob Ebey
FRC #3574 (High Tekerz)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2010
Location: Seattle
Posts: 101
jacob9706 is on a distinguished road
Re: Multithreading and locking?

Quote:
Originally Posted by rbmj View Post
I can attest to that. As soon as I took a second look at our code I realized that we'd had static initialization order issues, and sure enough, a few days later it segfaulted...

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
I was looking through your code and do you realize you can use namespaces?

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:oint(10,10) << std::endl;

You can now do this

cout << Point(10,10) << endl;

Just something I thought I would share with everyone.
__________________
/*
* Team 3574 Alumni
*
* 2011 - Highest Seeded Rookie
* 2011 - Rookie All-Star
* 2012 - Engineering Inspiration
* 2012 - Olympic Deans List Winner
* 2013 - Engineering Inspiration
* 2013 - Judges Award (For unique circular robot and the way the team works together.)
*/
Reply With Quote
  #8   Spotlight this post!  
Unread 12-02-2015, 21:17
pensono pensono is offline
Registered User
FRC #5495
 
Join Date: Jan 2015
Location: Snohomish, WA
Posts: 15
pensono is an unknown quantity at this point
Re: Multithreading and locking?

Quote:
Originally Posted by Skinkworks View Post
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.
As long as you only have one writer thread, you won't run into any problems.
Reply With Quote
  #9   Spotlight this post!  
Unread 12-02-2015, 21:25
PaulDavis1968's Avatar
PaulDavis1968 PaulDavis1968 is offline
Embedded Software/Systems Engineer
AKA: Master of Complexity
FRC #2053 (TigerTronics)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Endicot NY
Posts: 91
PaulDavis1968 is just really nicePaulDavis1968 is just really nicePaulDavis1968 is just really nicePaulDavis1968 is just really nicePaulDavis1968 is just really nice
Re: Multithreading and locking?

Quote:
Originally Posted by pensono View Post
As long as you only have one writer thread, you won't run into any problems.
Very true. Arguing this very thing at work at the moment. Any context switch were writing can only occur in one of the context events then you do not need to lock it to protect it from the reader/writer problem.
Reply With Quote
  #10   Spotlight this post!  
Unread 12-02-2015, 21:55
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Multithreading and locking?

Quote:
Originally Posted by pensono View Post
As long as you only have one writer thread, you won't run into any problems.
That depends on the behavior of the scheduler, and what the "writer" thread is writing to...


Reply With Quote
  #11   Spotlight this post!  
Unread 12-02-2015, 23:33
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,168
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Quote:
Originally Posted by pensono View Post
As long as you only have one writer thread, you won't run into any problems.
That depends on the variable. Try to manipulate a linked list without mutual exclusion and you are toast. Using a global double on a 32-bit machine might cause you trouble. If the global variable is simply an integer or a float and there is only one writer you should be ok.
__________________
Fast, cheap or working - pick any two!

Last edited by wireties : 12-02-2015 at 23:38.
Reply With Quote
  #12   Spotlight this post!  
Unread 12-02-2015, 23:35
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,168
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Quote:
Originally Posted by Ether View Post
That depends on the behavior of the scheduler, and what the "writer" thread is writing to...


The kernel we are using has all the RT extensions enabled so they could be in trouble.
__________________
Fast, cheap or working - pick any two!
Reply With Quote
  #13   Spotlight this post!  
Unread 18-02-2015, 12:18
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is offline
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,562
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
Re: Multithreading and locking?

Quote:
Originally Posted by pensono View Post
As long as you only have one writer thread, you won't run into any problems.
If you're not conscientious about read-once in a separate task, you can cause yourself problems again where obj.Value = 57 at the start of a function and obj.Value = 118 by the end, despite you not writing anything to it.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
Reply With Quote
  #14   Spotlight this post!  
Unread 03-08-2015, 12:10
knuckleduster knuckleduster is offline
Registered User
AKA: Jack Manning
FRC #0058 (The Riot Crew)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: South Portland
Posts: 12
knuckleduster is an unknown quantity at this point
Re: Multithreading and locking?

Quote:
Originally Posted by jacob9706 View Post
I was looking through your code and do you realize you can use namespaces?

so for example right after your includes you would put something like this...

using namespace cv;
using namespace std;

Just something I thought I would share with everyone.
Actually, it isn't really considered good practice to use namespaces, when you start using multiple libraries with similarly named functions, it can get hectic, and its just better to get into the habit of not using them if possible.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 10:05.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi