Is there a good tutorial on using threads in C?

  • Thread starter Thread starter Nissen, Søren Rune
  • Start date Start date
  • Tags Tags
    Threads Tutorial
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
Nissen, Søren Rune
I have a friend that needs something done which I can't think of a non-threaded way to do, and we're having some trouble understanding how the pthread library works.

He wants a program to execute until he presses a button. I suggested having one thread with a while loop that runs until a variable is changed, and a different loop that reads into that variable from the keyboard. Is this even a good solution to the problem?

If it is a good solution, what's the scheduler like? The guide I'm reading right now says

Thread Scheduling:

When this option is enabled, each thread may have its own scheduling properties. Scheduling attributes may be specified:

* during thread creation
* by dynamically by changing the attributes of a thread already created
* by defining the effect of a mutex on the thread's scheduling when creating a mutex
* by dynamically changing the scheduling of a thread during synchronization operations.

The threads library provides default values that are sufficient for most cases.
but that doesn't tell me what happens if I don't enable thread scheduling. I'm guessing the thread that executes first will then keep running until it's finished (which it never will) and the other one will be locked out but frankly I'm not sure at all.
 
Physics news on Phys.org
If the threads are created with different priorities, then the higher priority thread will always run until it makes a scheduler call to "wait" for some scheduler supported form of synchronization, such as a mutex. Although not part of the pthread standard, but part of POSIX 1b, there are also semaphores, which are similar to mutexes with counts instead of just being true/false. If the scheduler includes time slicing, then it will cycle between threads that have the same priority (assuming all higher priority threads are "waiting" (in a sleep state) for something), on a periodic basis, usually a few milliseconds for each thread. Some advanced schedulers will temporarily increase the priority of a thread, based on how long it's been ready to run, but locked out by higher piority threads.

http://en.wikipedia.org/wiki/POSIX_Threads

http://en.wikipedia.org/wiki/Semaphore_(programming)

I wrote an example Windows multi-threaded application (it just copies a file), showing how to use multiple threads, mutexes, semaphores, wait for objects, and a link list fifo messaging system to pass messages (the messages mostly contain pointers to buffers) between threads. The mutexes are used to control "ownership" of message fifos, and semaphores are used to keep track of how many messages are in a fifo. One nice feature in windows is waitformultipleobjects() (used in GetFirstElement() in this example), which I use to wait for both a mutex and a semaphore, so in a single atomic (non-interruptable) call to the OS, a thread can wait for both ownership and a non-zero semphore count, then decrement the sempaphore count, which eliminates any issues of priority between fifo sending and fifo receiving threads (preventing a deadlock situation).

The overhead for creating and releasing this stuff is more complicated in windows, but once done, note how simple the actual thread code in the functions ThreadFile0() and ThreadFile1() are.

http://rcgldr.net/misc/mtcopy.zip
 
Last edited:
Sure enough, we ended up doing it with threads. pthread_create and its fuzzy little family of functions. In the end, the part that was the most difficult was to get the IDE (Eclipse, in this case) to understand what -pthread meant in the compiler call. It's all handled now though.

Thanks for uploading your examples though, I'm sure they'll help me with this in the future when I move from *nix to windows, or when I have to explain to my friend how to do it. :)