Table of Contents
Below is a brief tutorial on using threads in Linux with C
. This tutorial will cover the basics, including creating threads, waiting for them to finish, and some important considerations. I'll provide an example that demonstrates these concepts.
What Are Threads?
Threads are a way for a process to allow multiple flows of execution. This can improve the performance of an application by parallelizing its operations. In Linux, threads are lightweight processes (LWP). The POSIX thread libraries (pthreads) is a standards based thread API for C/C++
, which we'll use in this tutorial.
Setting Up Your Environment
To work with pthreads, you need to have the pthread
library available on your system. Most Linux distributions come with this library by default. You'll include the pthread library in your C
code and link against it during compilation.
Basic Concepts
- Creating a Thread: You create threads using
pthread_create()
. - Waiting for a Thread to Finish: You wait for a thread to finish by calling
pthread_join()
. - Passing Data to Threads: You can pass data to threads by sending a pointer to the data as an argument to
pthread_create()
. - Exiting a Thread: A thread can exit by returning from its start routine or by calling
pthread_exit()
.
Example
Let's create a simple program that starts a thread. This thread will print out a message.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
// Thread start function
void* print_message_function(void* ptr) {
char *message;
message = (char *) ptr;
printf("%s \n", message);
return NULL;
}
int main() {
pthread_t thread1;
const char *message1 = "Thread 1";
int thread1_return;
// Create the thread
thread1_return = pthread_create(&thread1, NULL, print_message_function, (void*) message1);
if (thread1_return) {
printf("Thread creation failed: %d\n", thread1_return);
exit(EXIT_FAILURE);
}
// Wait for the thread to finish
pthread_join(thread1, NULL);
exit(EXIT_SUCCESS);
}
The output console should display the thread name in this cas Thread 1
The plantuml for this code is the following
@startuml
!theme toy
participant "main()" as main
participant "pthread_create()" as pcreate
participant "print_message_function()" as pfunction
participant "pthread_join()" as pjoin
participant "exit()" as pexit
main -> main : Define thread1\nDefine message1 = "Thread 1"
main -> pcreate : Create thread1\nwith message1
activate pcreate
pcreate --> main : thread1_return
main -> pfunction : Execute in thread1
activate pfunction
pfunction -> pfunction : Print message1
deactivate pfunction
main -> pjoin : Wait for thread1 to finish
activate pjoin
pjoin --> main
main -> pexit : EXIT_SUCCESS
deactivate main
@enduml
Compiling and Running Your Program
To compile this program, you'll need to link it with the pthread library. Here's how you can do it:
gcc -o thread_example thread_example.c -lpthread
After compiling, you can run your program like any other:
./thread_example
Notes and Best Practices
- Always check the return values of pthread functions for errors.
- Remember to join threads that you create (unless you specifically want them to run detached).
- Use mutexes or other synchronization methods to protect shared data.
- When passing data to threads, ensure the data's lifetime is sufficient (it must remain valid until the thread is done with it).
This tutorial covers the very basics to get you started with threads in Linux using C. There's much more to learn, including thread synchronization, specific data management, and more advanced patterns of usage. I encourage you to explore more detailed resources and official documentation as you get more comfortable with these basics.
🏷️ Author position : Embedded Software Engineer
🔗 Author LinkedIn : LinkedIn profile
Comments