What is a worker thread?
in
Programming Questions
•
1 year ago
I'm relatively new to processing. I got a little help with a code via e-mail today but the solution to my problem involved using a "worker thread". I am not familiar with this term and did not find much info searching the forum. Could one of you offer a general explanation of what this term means and how it is applied to a processing sketch??
Here is a snipet of the message. It makes rough sense to me but I dont understand it well enough to implement it into my own sketches. If a worker thread is nothing more than a seperate function then I don't see how putting the delay mentioned below there would change anything since the program would just branch off into that function and run the delay anyways.
[MESSAGE]:
I just quickly looked into your code, but the main problem for your
speed problem is that you have a delay in your drawing loop.
This way you slowdown your sketch pretty bad(by 111ms every frame) !
Check out your method PointDrawer.draw():
if(firstVec != null)
{
strokeWeight(8);
context.convertRealWorldToProjective(firstVec,screenPos);
point(screenPos.x,screenPos.y);
velocity= 150-int(screenPos.y)/3; pitch = int(screenPos.x)/5-3;
print(screenPos.y); print(" Y ");println(velocity);
print(screenPos.x); print(" X");println(pitch);
myBus.sendNoteOn(channel, pitch, velocity);
// Send a Midi noteOn
delay(111); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< not a good idea
myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi
nodeOff
}
in case you want to send of a note without delaying the rendering/ data
processing part, i would suggest that you use a
simple worker thread which just waits to send of those notes. From the
SimpleOpenNI thread you just give a message to
the worker thread(which only waits for those events), the worker thread
then sends you the notes.
The worker thread just has a stack/list/queue of commands(like the note
off/on, channel, pitch, velocity, timestamp etc.), in the main loop of
this thread you just send out those commands, depending on their timestamp.
For example, this could be the command:
sendNote(int sendTimeOffset, boolean noteOnOffMode, int channel,
int pitch, int velocity)
in the code:
sendNote(0,true,channel,pitch,velocity);
sendNote(111,false,channel,pitch,velocity);
The command would write those infos into a queue in your working thread.
In this way you have no interruption in your drawing thread and you can
send of that many notes you want, without any delay.
speed problem is that you have a delay in your drawing loop.
This way you slowdown your sketch pretty bad(by 111ms every frame) !
Check out your method PointDrawer.draw():
if(firstVec != null)
{
strokeWeight(8);
context.convertRealWorldToProjective(firstVec,screenPos);
point(screenPos.x,screenPos.y);
velocity= 150-int(screenPos.y)/3; pitch = int(screenPos.x)/5-3;
print(screenPos.y); print(" Y ");println(velocity);
print(screenPos.x); print(" X");println(pitch);
myBus.sendNoteOn(channel, pitch, velocity);
// Send a Midi noteOn
delay(111); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< not a good idea
myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi
nodeOff
}
in case you want to send of a note without delaying the rendering/ data
processing part, i would suggest that you use a
simple worker thread which just waits to send of those notes. From the
SimpleOpenNI thread you just give a message to
the worker thread(which only waits for those events), the worker thread
then sends you the notes.
The worker thread just has a stack/list/queue of commands(like the note
off/on, channel, pitch, velocity, timestamp etc.), in the main loop of
this thread you just send out those commands, depending on their timestamp.
For example, this could be the command:
sendNote(int sendTimeOffset, boolean noteOnOffMode, int channel,
int pitch, int velocity)
in the code:
sendNote(0,true,channel,pitch,velocity);
sendNote(111,false,channel,pitch,velocity);
The command would write those infos into a queue in your working thread.
In this way you have no interruption in your drawing thread and you can
send of that many notes you want, without any delay.
1