quick intro to multithreaded programming



Children, Kids and Teens: listen up, I am only going to tell you this once, because it's important for your brain health, which is important for your entire body - do not do multithreaded programming just because it's available to you. 20-something's you can. Do not do multithreaded programming until college. and do not try to go to college too early. if you get headaches, it probably means one of several things (but I have learned this by experience):

  • for engineers and techs, , carbs like pasta, bread, pastries, fats, and sweets to match your body type (if your head is hot, try some)
  • lack of food
  • lack of safe drinking water (drink until you are no longer thirsty) (techs and engineers too) distilled water is far too acidic for people to drink and is only for machinery and checmical processes and can hurt your insides over time.
  • lack of minerals

you should focus on school while you have it (unless you are college age and need that kind of work) and just being who you are and being your own age.

multithreading has its uses, but it is not typical programming meaning it is the exception rather than the rule. it can be used in a GUI to provide responsiveness. you can process tasks, windows has this concept already. here is a quick beginner's introduction.

find out number of cpu threads, number of available procs

find out number of cpu threads, number of available threads via std::thread? nope(10/15/2014).

there is no std::threads way of querying the number of procs. I wish there were. you could do better thread management that way. especially if you could find out how many available threads there are - then you could say "I need this percentage of those available threads because I want my process to be nice" or "I need this specific quantity of available threads".

Linux proc count

OSX and linux processor count

OSX proc count

OSX and linux processor count

Windows proc count

windows processor count
windows WaitForMultipleObjects() - in a manager thread when you are done procesing you can wait for all the threads to quit.

GetActiveProcessorCount() which you can subtract from the processor count to get available processor (thread) count.


windows' jobs are a polling try-fail-try-again-until-you-get-a-job-started (broken) thing where you attempt to start a thread, and if it succeeds, great. if it doesn't you sleep for a while and try again. the sleep is so you don't chew up a proc/thread/core in the management thread, which blocks so the OS can do stuff with that cpu thread in the meantime if the that cpu thread is needed.

and there's mutexes, semaphores, and all sorts of locking semantics you can do for concurrency, with native threads, and possibly OpenMP.

there's the JS Web Workers multithreading API I haven't had much personal need to use it lately, but it looked useful for one web page I had that did the rain program type of thing, but you would quickly run out of CPU threads and didn't get it to run on ff at the time since it was a new API at the time, so I changed it to a timer event callback thing which works much better and handles more concurrency and is more fluid.

you can peg 100% cpu usage on a 1-thread proc and use up or slow down the whole machine by using a busy-wait with no Sleep() to block for the OS or other programs to use the thread. using a Sleep() in the loop would knock that way down, possibly to 1%.

if you do multithreading, you should buy a book or do some online reading on the subject, since there are complexities. for instance:
if 2 threads (what about 3?)are concurrently accessing or alternately accessing a structure or variable, like the example of A and B threads both doing a read-increment-write, doing this without a semaphore or mutex can lose data - it should also be noted that semaphores (locks) can cause deadlock conditions, since the other threads are blocking (waiting) to access a single resource. in C/C++, you should declare it volatile at least (I think there are new std c++ lib declarations now possibly, but this is the only one I know of offhand, I forgot the new ones and have not looked into them in depth), so the compiler knows something might pop in an unexpectedly change the value.