Simple question about threads

I’ve been using threads for years, but I still have these doubts:

Imagine I have two threads with the same priority.

1st question:
Is it true that they share system time 50%?

2nd question:
Assuming that the first question is true, if one of the threads has a very long loop, will they still share time? Or a Loop is never stopped?
I know that with timers, loops prevail, but I don’t know with threads.

Thanks.

Both threads will have the same number of time units in the app. These threads also share time with the main (UI) thread. Remember Xojo apps only run on a single CPU core, so this does not affect the system as a whole.

Threads can yield time to other threads each time they execute a looping construct such as For, While, and Do. However, a thread does not necessarily yield time at every loop boundary even though it has an opportunity to do so.

You can also call Application.YieldToNextThread if you want to be more specific.

More information here:
http://developer.xojo.com/userguide/threading

Threads actually yield time at loop boundaries (among other places, I’m sure) so if you have a thread method that does one thing without looping (or calling other functions, I suppose), you might as well skip the thread.

But all of this is an implementation detail that is subject to change. I guess you can assume 50% sharing, but it will never be exact and might not even break the same way each time. If you need to control thread flow, use YieldToNextThread to force a context change somewhat and Semaphores/CriticalSections to keep one Thread’s work from clobbering another’s.

@Kem Tekinay / @Paul Lefebvre

when I have 2 threads like Ramon said, what happens to the 2nd thread if the 1st has just starting to query data from a remote database which takes relatively long and depends on a plugin ? Will the execution of the 2nd thread being obstructed by the 1st one ?

It certainly could be forced to wait if the DB call itself does not yield.

The Postgres plugin yields when the MultiThreaded property = True (default). SQLite has the ThreadYieldInterval which needs to be set. MySQL doesn’t block threads at all according to the LR.

This helps. Didn’t know.

Thanks Paul and Kem.
I think I have now a better vision, but I certainly must check it to be completely confident.

Are the threads actual OS threads, or are they something kept by the runtime ?