Is there a good tutorial on using threads in C?

In summary, the conversation discusses a problem involving executing a program until a button is pressed. The suggested solution involves using threads and mutexes, with the possibility of using semaphores as well. The conversation also mentions the role of thread scheduling and provides examples for both POSIX threads and Windows multi-threaded applications. The conversation concludes with the resolution of the problem and gratitude for the provided examples.
  • #1
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
  • #2
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:
  • #3
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. :)
 

1. What are threads in C and why are they important?

Threads in C are a way for a program to execute multiple tasks simultaneously. This is important for programs that need to handle multiple processes or tasks at the same time, as it can improve performance and efficiency.

2. Is there a specific tutorial for using threads in C?

Yes, there are many tutorials available online for using threads in C. Some popular ones include the tutorial on the C programming language website and the tutorial on GeeksforGeeks.

3. Can you explain the basic syntax for creating and managing threads in C?

The basic syntax for creating and managing threads in C involves using the pthread_create() function to create a new thread and the pthread_join() function to wait for the thread to finish its execution. Thread synchronization can be achieved using functions like pthread_mutex_lock() and pthread_mutex_unlock().

4. What are some common challenges when using threads in C?

Some common challenges when using threads in C include managing thread synchronization and avoiding race conditions, ensuring proper memory management, and debugging issues related to concurrent execution.

5. Are there any best practices for using threads in C?

Yes, some best practices for using threads in C include properly managing thread synchronization, using thread-safe functions and data structures, and avoiding excessive use of threads. It is also important to properly handle errors and exceptions when working with threads.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
330
  • Programming and Computer Science
Replies
22
Views
919
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Feedback and Announcements
Replies
21
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top