0

I am making an application in C that depends on asynchronous method calls. In my best efforts thus far to make it cross platform I am currently implementing code that looks like this:

#ifdef TARGET_WINDOWS
//Windows threading API
#else
//pthreads API
#endif

I appreciate there is probably a better way to do that but that is not the point of my question. My question is, what will happen to the program on a single-core processor? Will the threads still execute asynchronously (interleaved instructions perhaps), or am I stuck with single-thread execution only on single-core CPUs?

2
  • "interleaved instructions perhaps" -- people have answered yes to this, which is fair enough, but just to get your mental model straight bear in mind that it won't alternate instructions (well, it can and you could argue that's what hyper-threading is, but it's not the general model for multi-tasking on a single core). A time-slice will most likely be a "significant" fraction of a second, I think Linux typically varies 10-300 ms. and Windows near the low end of that. More like inter-tree-ing than inter-leaf-ing, since that could be time for millions of instructions. Oct 21, 2012 at 13:11
  • @SteveJessop I have some clarifying questions. What happens when threads of non-equal priority are run. Does the low priority thread run only after high priority thread is finished? (Given that man7.org/linux/man-pages/man7/sched.7.html says: "A thread's scheduling policy determines where it will be inserted into the list of threads with equal static priority and how it will move inside this list."). While to me it seems, even low priority threads get some time.
    – qqqqq
    Jan 19, 2016 at 20:02

3 Answers 3

2

At the end of the day a single core can only run one thread at a time. The thread scheduler will interrupt the running thread and give another thread the chance to run for the specified quantum.

Now what happens exactly is up to the OS. But should all your work be CPU bound, maybe some intensive calculations then the overall performance may in fact degrade with more threads due to the cost of context switching. But if you are waiting on I/O for example, the OS can schedule another thread to run in the meantime to make use of CPU.

1
  • Does OS can schedule another thead in a single-core processor ?
    – Praneeth
    Feb 3, 2016 at 20:04
1

The threads will execute concurrently, even on a single core. This is because of preemptive scheduling, which means that the OS will pause threads to let other threads run.

This is true for all the common desktop OSes. However, embedded/"real time"-OSes might be different.

0

Threads on a single core CPU each get tiny timeslices from the OS in turn, depending on a few factors such as static and dynamic priority, the type of process they're running in, etc. Point is, they will run in a parallel(ish) fashion (concurrently in fact) by interleaving their executions on the single CPU.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.