Thread is being Stopped - multithreading

I am currently attempting to run a separate thread in the Unity 3D to calculate a long path using A*. I do have the thread running and it does not utilize the unity API so it doesn't cause any conflicts but for some reason it seems to stop and I can't figure out why.
while(!_threadKilled)
{
if(_flockers.Count > 0)//still flockers left to calculate a path for
{
Flocking flocking = _flockers.Peek();
float x = _xPositions.Peek();
float z = _zPositions.Peek();
_flockers.Dequeue();
_xPositions.Dequeue();
_zPositions.Dequeue();
flocking.FollowNewPath(_game.Pathfinding.CalculatePath(x,z,flocking.TargetTile));
Thread.Sleep(500);
}
}
I have in a update function the state of the thread being constantly printed out. When I add one unit to the list it works perfectly fine. When I add multiple units to the list the thread seems to stop. Anyone have any idea what going on?

Related

Visual c++ infinite while loop

In visual c++ in express 2013 for Windows desktop I made a ASCII roguelike but the following code is messing it all up.in the game when u defeat.a enemy you get experience which get added to players and levels up if enough.here is the code.it is in a player class I created.
It printed Leveled up to .. no of times I press any key like the while loop never ends .presses a key leveled appears then again I presses a key leveled up appears it keeps on going
void Player::addExperience(int experience){_experience += experience;
while(_experience >50) {
_experience -= 50;
_level++;
printf("Leveled up%s",_level);
_getch();
}
}
When I changed the code to ->
void Player::addExperience(int experience) {_experience += experience;while(_experience >50) { _experience = 0;_level++/printf("Leveled up%s",_level);
_getch();
}
}
It printed only one time leveled up to .. So I know the problem is in this code but what. don't know.
I re-read your description few times to understand your problem. I hopt I understood it correctly.
We don't know the value of experience. Unless it keeps chaning in the background, the loops are supposed to run infinite number of times OR zero times based on value of experience.
The first fragment of code increments level by 1 each time it decreases _experience by 50. But, the while loop checks for experience instead of _experience, so the loop never breaks.

Qt5: How to create a (cleanup) task that runs once per day at 3:00am? [duplicate]

This question already has an answer here:
How to emit a Qt signal daily at a given time?
(1 answer)
Closed 7 years ago.
I am using Qt5 under Windows7.
I know how to create a task using QThread, but my problem is:
How do I run it every day at 03:00AM?
I was thinking about QTimer, but it doesn't seem to be ok... it can't be linked somehow to 03:00am.
Just to make it clear: I can't use some Windows application(s). It must be coded inside my Qt app as it does some cleaning job too: cleanup history list, trim it down to 1000 lines (or whatever), etc. So, you see I can't do that using TaskScheduler or similar Windows tools...
you can use windows task scheduler to do this for you
Whats wrong with using a QTimer? I agree that a task scheduler is the better option. Here, only about 0,03% of the time code is executed it is really supposed to do something. If the exact moment is not as important you can increase the timer interval and the check-boundaries and reduce the unncessary calls. But if you prefer such a solution this should work:
someclass::someclass(){
member_timer = new QTimer(this);
QObject::connect(member_timer, SIGNAL(timeout()), this, SLOT(check_time()));
member_timer->start(30000);
member_cleanup_performed = false;
}
void someclass::check_time(){
QTimer ctime = QTime::currentTime();
if(ctime.hour() == 3 && ctime.minute() == 0){
if(member_cleanup_performed == false){
this->cleanup();
member_cleanup_performed = true;
}
}else{
member_cleanup_performed = false;
}
}
If you can use C++11, have a look at std::this_thread::sleep_until.
Run it in a separate thread and let the thread emit a signal connected to a slot in the main thread, which then performs the action. That of course requires that your application is actually running at 3 am.

RobotC: Multithreading in TeleOp (controlling mode)

I am making a program for a robot in a competition, and need to multithread.
When I make a second task (task two()) and try to start (startTask) it with a button press from a controller, it just executes the first statement of the task and only as long as the button is pressed, instead of the whole block. I've tried many things including putting a loop in the second task also, using a function instead of a task and sleeping for 200 milliseconds before, and after the startTask(two); function, but the same thing happens every time.
I can't post my program because I don't want other people to steal it, sorry.
What edits will make it run the whole block?
Any help would be appreciated.
Since this is Controller Mode, I'm assuming that you are setting the motors to stop when the corresponding button is not pressed.
if([...])
[...]
else
{
setMotorSpeed(motor10, 0);
}
This is the cause for the stopping of the motors when you release. All of the other methods that you tried had nothing to do with this, so they shouldn't have worked.
You need to put something like this:
int Motor10Speed;
[...]
if([...])
[...]
else
{
setMotorSpeed(motor10, Motor10Speed);
}
This will control an individual motor. Repeat this for all other motors being used.
After that is done, make the function look something like this:
task mini_function();
task main()
{
[...]
}
task mini_function()
{
Motor10Speed = 50;
sleep(1000);
Motor10Speed = 0;
}
Expand the above program so it matches your current function, while using the MotorSpeed variables as setMotorSpeed variables.
This should make you able to drive and run a function at the same time without them interrupting each other.

How to crash my Node app on purpose?

I've been working on a deployment work flow with Dokku and Docker and now I want to take care of continuity of my app (along the lines of Forever). To test it, I need a way to deliberately crash my app.
I created a new route '/crashme' with a function that is supposed to wreck my app.
Haven't found a way that worked locally with node/nodemon so far, I've tried:
Division by zero
Throw a new user exception
Referencing a variable that doesn't exist
None of those things crash the app to a point where it needs to be restarted.
Just how can I bring it down?
Three things come to my mind:
You could just call process.exit. This for sure brings your application to a state where it needs to be restarted.
The other option might be to run an endless loop, something such as while (true) {}. This should make Node.js use 100% of your CPU, and hence the application should be restarted as well (although this, of course, means that you / someone has to watch your application).
Create a module in C that crashes by e.g. trying to access a random place in memory. I have no such module at hand, but I'm pretty sure that it should be quite easy for someone with C skills to write such a module.
I was attempting a similar thing with a /crash route in express, but just throwing an error from within the route handler was not enough to crash it.
process.exit would stop my app but forever would not restart it. (The forever logs just said something like process self terminated.)
What did work for me was inserting this into my /crash route:
setTimeout(function () {
throw new Error('We crashed!!!!!');
}, 10);
To add to Golo answer:
C module to crash by segmentation fault:
int main ()
{
//Create a array of 1 char
char a [1];
//Create a index
int i = 0;
//Infinite loop to go around the compiler
while(1)
{
//Write on case i of a, on the second iteration, it will write in unreserved memory => crash
a[i] = 0;
i = i + 1;
}
//Should not go there
return -1;
}
And adding to DrakaSAN's answer, an even simpler C module to crash:
int main()
{
*(int*)(0) = 0;
return -1;
}
Even shorter ones are available on this page. If you don't want it to be too hard to read, you can probably go with
int main()
{
int i=1/0;
}

FMOD surround sound openframeworks

Ok, I hope I don't mess this up, I have had a look for some answers but can't find anything. I am trying to make a simple sampler in openframeworks using the FMOD sound player in 3D mode. I can make a single instance work fine (recording a new file using libsndfilerecorder and then playing it back and moving it in surround.
However I want to have 8 layers of looping audio that I can record and replace one layer at a time in a live show. I get a lot of problems as soon as I have more than 1 layer.
The first part of my question relates to the FMOD 3D modes, it is listener relative, so I have to define the position of my listener for every sound (I would prefer to have head relative mode but I cannot make this work at all. Again this works fine when I am using a single player but with multiple players only the last listener I update actually works.
The main problem I have is that when I use multiple players I get distortion, and often a mix of other currently playing sounds (even when the microphone cannot hear them) in my new recordings. Is there an incompatability with libsndfilerecorder and FMOD?
Here I initialise the players
for (int i=0; i<CHANNEL_COUNT; i++) {
lvelocity[i].set(1, 1, 1);
lup[i].set(0, 1, 0);
lforward[i].set(0, 0, 1);
lposition[i].set(0, 0, 0);
sposition[i].set(3, 3, 2);
svelocity[i].set(1, 1, 1);
//player[1].initializeFmod();
//player[i].loadSound( "1.wav" );
player[i].setVolume(0.75);
player[i].setMultiPlay(true);
player[i].play();
setupHold[i]==false;
recording[i]=false;
channelHasFile[i]=false;
settingOsc[i]=false;
}
When I am recording I unload the file and make sure the positions of the player that is not loaded are not updating.
void fmodApp::recordingStart( int recordingId ){
if (recording[recordingId]==false) {
setupHold[recordingId]=true; //this stops the position updating
cout<<"Start recording Channel " + ofToString(recordingId+1)+" setup hold is true \n";
pt=getDateName() +".wav";
player[recordingId].stop();
player[recordingId].unloadSound();
audioRecorder.setup(pt);
audioRecorder.setFormat(SF_FORMAT_WAV | SF_FORMAT_PCM_16);
recording[recordingId]=true; //this starts the libSndFIleRecorder
}
else {
cout<<"Channel" + ofToString(recordingId+1)+" is already recording \n";
}
}
And I stop the recording like this.
void fmodApp::recordingEnd( int recordingId ){
if (recording[recordingId]=true) {
recording[recordingId]=false;
cout<<"Stop recording" + ofToString(recordingId+1)+" \n";
audioRecorder.finalize();
audioRecorder.close();
player[recordingId].loadSound(pt);
setupHold[recordingId]=false;
channelHasFile[recordingId]=true;
cout<< "File recorded channel " + ofToString(recordingId+1) + " file is called " + pt + "\n";
}
else {
cout << "Sorry track" + ofToString(recordingId+1) + "is not recording";
}
}
I am careful not to interrupt the updating process but I cannot see where I am going wrong.
Many Thanks
to deal with the distortion, i think you will need to lower the volume of each channel on playback, try setting the volume to 1/8 of the max volume. there isn't any clipping going on so if the sum of sounds > 1.0f you will clip and it will sound bad.
to deal with crosstalk when recording: i guess you have some sort of feedback going on with the output, ie the output sound is being fed back into the input channel, probably by the operating system. if you run another app that makes sound do you also get that in your recording as well? if so then that is probably your problem.
if it works with one channel, try it with just 2, instead of jumping straight up to 8 channels.
in general i would try to abstract out the playback/record logic and soundPlayer/recorder into a separate class. you have a couple of booleans there and it's really easy to make mistakes with >1 boolean. is there any way you can replace the booleans with an enum or an integer state variable?
EDIT: I didn't see the date on your question :D Suppose you managed to do it by now. Maybe it helps somebody else..
I'm not sure if I can answer everything of your question, but I can share how I've worked with 3D sound in FMOD. I haven't worked with recording though.
For my own application a user can place sounds in 3D space around himself. For this I only have one Listener and multiple Sounds. In your code you're making a listener for every sound, are you sure that is necessary? I would imagine that this causes the multiple listeners to pick up multiple sounds and output that to your soundcard. So from the second sound+listener, both listeners pick up both sounds? I'm not a 100% sure but it sounds plausible to me.
I made a class to create sound objects (and one listener). Then I use a vector to store the objects and move trough them to render them.
My class SoundBox basically holds all the necessary things for FMOD
Making a "SoundBox" object and adding it to my soundboxes vector:
SoundBox * box = new SoundBox(box_loc, box_rotation, box_color);
box->loadVideo(ofToDataPath(video_files[soundboxes.size()]));
box->loadSound(ofToDataPath(sound_files[soundboxes.size()]));
box->setVolume(1);
box->setMultiPlay(true);
box->updateSound(box_loc, box_vel);"
box->play();
soundboxes.push_back(box);
Constructor for the SoundBox. I use a similar constructor in the same class for the listener, but since the listener will always be at the origin for me, it doesn't take any arguments and just sets all the listener locations to 0. The constructor for the listener only gets called once, while the one for the Sound gets called whenever I want to make a new one. (don't mind the box_color. I'm drawing physical boxes in this case..):
SoundBox::SoundBox(ofVec3f box_location, ofVec3f box_rotation, ofColor box_color) {
_box_location = box_location;
_box_rotation = box_rotation;
_box_color = box_color;
sound_position.x = _box_location.x;
sound_position.y = _box_location.y;
sound_position.z = _box_location.z;
sound_velocity.x = 0;
sound_velocity.y = 0;
sound_velocity.z = 0;
Then I just use a for loop to loop trough them and play them if they're not playing. I also have some similar code to select them and move then around.
for(auto box = soundboxes.begin(); box != soundboxes.end(); box++){
if(!(*box)->getIsPlaying())
(*box)->play();
}
I really hoped this helped. I'm not a very experienced programmer but this is how I got FMOD with multiple sounds to work in OpenFrameworks and hope you can use some of it. I just dumped as much of my code as I could :D
My main suggestion is to make one listener instead of more. Also having a class for making the sounds is useful if you, for instance, want to relocate the sounds after the initial placement.
Hope it helps and good luck :)

Resources