473,326 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Add Control over recursive function

Hello All, below i attached a code where i need a control on recursive function. Means, it should wait for my button press signal to move further.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void PlaySongs::performance(int step){
  3.  
  4. step++;
  5.  
  6. if (waitForInterruptSys (iOnePin, -1) >0)
  7. {
  8.    cout<<"Push Button is pressed"<<endl;
  9.  
  10.    SoundNamespace::SoundProject::SoundProject project;
  11.  
  12.    project.Init();
  13.    project.InitSound();
  14.  
  15.    project.StartInterruptThreads();
  16.    project.LoadProject();
  17.    project.AdjustSounds();
  18.    project.LoadSounds(true);
  19.    project.StartPlayingSounds();
  20.  
  21.    cout << "Initialization finished." << endl;
  22.  
  23.  
  24. // Some C++ control statement NEEDS here  <--
  25.  
  26. /* here i need control, so that, after button pressed on bread board, it should move further
  27. i.e. perform next statements, otherwise wait infinite time. something like cvWaitKey(0) from
  28. opencv. Interrupt detected by (waitForInterruptSys (iOnePin, -1) >0). like written above. */.
  29.  
  30. if(step==3)
  31.      return;
  32. else
  33.     performance(step);
  34.  
  35. }
  36. }
  37.  
  38.  
  39.  
Aug 14 '15 #1
1 1290
weaknessforcats
9,208 Expert Mod 8TB
Your easiest solution is to use a global static variable.

Say you put 0 into the variable and then the recursive function is called. The recursive function initially goes into a loop which tests the variable. When the value is , say 1, the function proceeds. Each recursion call will test the same variable, which will be 1, and so the recursion will proceed normally.

At the end, the caller of the recursion changes the value back to 0. The recursive function should not change the variable.

This is a "poor man's" version of a Singleton object. It's OK for student work but not for actual production code.

Unfortunately, while that recursive function is in a loop which tests that global variable, it has tied up the thread of execution so the caller of the recursion can't get in to change the variable. Therefore, you call your recursive function on a separate thread. Here you need to research, for the OS you are using, how to do multithreading.

Worse, with both the recursive function thread and the caller thread of the recursion trying to access that global variable at the same time, you can create a race condition that can lock up your program. So you will also need to learn how to set up, and use, a critical section to surround access to that variable so that only one thread at a time can access it.

What OS are you using?
Aug 15 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
8
by: Rick | last post by:
I have the recursive function below which I use to find a datagridview with a certain bindingsource. When the grid is parented by a control with only one child (the dgv) the function works ok,...
2
by: Iamrashmin | last post by:
I have converted VC6 project to .Net 2005 VC++. It gives me warning : warning C4717: 'CExcel::PrintLine' : recursive on all control paths, function will cause runtime stack overflow void...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
2
by: neovantage | last post by:
Hi Geeks, I am using adodb for accessing my records from MYSQL Database. I define an object of adodb "$db" which is used for executing queries in my configuration file which is accessible in each...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.