Passing variables in threads by ChatGPT

String[] array;
int value;
Thread thread;

void setup() {
  // Initialize the array and value
  array = new String[10];
  value = 42;

  // Start a new thread to save the file
  thread = new Thread(new Runnable() {
    public void run() {
      saveFile(array, value); }});
  thread.start();
}

void draw() {
  // Do something else while the file is saving
}

void saveFile(String[] array, int value) {
 saveStrings("Generic file"+str(value)+".txt",array);println("Done");
}

This seems to work…I needed to write files but I didnt want the code to stop every time I needed to write a file…My question is can this be improved…or am I doing anything naughty :saluting_face:
https://chat.openai.com/chat

Edit Thanks to GoToLoop the saveFile thread should be:

void saveFile(String[] loc_array, int loc_value) {
 saveStrings("Generic file"+str(loc_value)+".txt",loc_array);println("Done");}
1 Like

Yeah but the normal way shown in the reference does not allow for passing variables or arrays…already trried that. If you have a better solution then I would like to see it…of course I am not calling this thing in set up…just showing as an example and want verification that this is the correct way of doing it.

That’s right! Same way both setup() & draw() and other Processing callbacks are parameterless.

Notice that both array & value are already global variables in your sketch.

So what’s the point of passing anything to your threaded function?

2 Likes

its just the most basic example to show it working. In my program I am playing around with a quadratic sieve…so I need to save a bunch of variables or else the computer will run into memory problems. Also the state of those variables is important…if I use global variables then the array could change before I write the file.

In your posted code you’re passing a global variable as saveFile()'s 1st parameter.

It means saveFile()'s 1st parameter is referring to the same object as the global variable argument.

Changes to the actual array object will reflect on both the global variable & local parameter.

In short, parameters don’t freeze the content of an object passed argument.

1 Like

excellent point!!!

So the code should be:

void saveFile(String[] loc_array, int loc_value) {
 saveStrings("Generic file"+str(loc_value)+".txt",loc_array);println("Done");}

In my code I didnt make this mistake…but thanks for the advice it is very relevant to the example code.

I stumbled across a bug in my code…it turns out that when you pass the array and the size of the array…it takes time to spin up the thread and in the mean time those values can change…so you need to pass a copy of those instead of the original…

Then invoke clone() method over the array: saveFile(array.clone(), value);

1 Like

I tried that and it has the same exact problem. So we need to create another function to pass the array and array index.

void startThread(String[] loc_array, int loc_value) {
  thread = new Thread(new Runnable() {
    public void run() {
      saveFile(loc_array, loc_value); }});
  thread.start();}

I dont know how to edit my original post…

Now my program finally runs correctly…thanks for the help

Now I am going to port my code to Julia and try running it on a virtual machine