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
Click For Summary
SUMMARY

This discussion focuses on using the pthread library in C for implementing multi-threading solutions. The user suggests a design where one thread runs a continuous loop until a variable is changed by another thread that reads keyboard input. Key points include thread scheduling attributes that can be set during thread creation or modified dynamically, and the importance of mutexes and semaphores for managing thread synchronization. The user also shares an example of a Windows multi-threaded application that utilizes mutexes and semaphores effectively, highlighting the complexities of thread management across different operating systems.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with POSIX threads (pthread) library
  • Knowledge of thread synchronization mechanisms such as mutexes and semaphores
  • Basic understanding of thread scheduling concepts
NEXT STEPS
  • Research "pthread_create" and its associated functions for thread management
  • Explore thread scheduling attributes in the pthread library
  • Learn about mutexes and semaphores in POSIX for thread synchronization
  • Investigate differences in multi-threading implementations between Windows and Unix-like systems
USEFUL FOR

Software developers, particularly those working with C and multi-threaded applications, as well as anyone interested in understanding thread synchronization and scheduling in programming environments.

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.
 
Technology 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. :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
4K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
4
Views
3K