在 Linux 中,可以使用 pthread 库来创建线程。以下是一个简单的示例:

1. 首先,需要包含头文件 `#include
2. 然后,定义一个线程函数,该函数将在新线程中执行。线程函数的原型如下:
```c
void *thread_function(void *arg);
```
3. 接下来,创建一个线程对象,并使用 `pthread_create()` 函数启动线程。`pthread_create()` 函数的原型如下:
```c
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
```
4. 最后,使用 `pthread_join()` 函数等待线程结束。`pthread_join()` 函数的原型如下:
```c
int pthread_join(pthread_t thread, void **retval);
```
下面是一个完整的示例:
```c
#include
#include
#include
void *thread_function(void *arg) {
printf("Hello from thread %ld
", (long)arg);
return NULL;
}
int main() {
pthread_t thread;
int result;
result = pthread_create(&thread, NULL, thread_function, (void *)1L);
if (result != 0) {
fprintf(stderr, "Error creating thread: %d
", result);
exit(EXIT_FAILURE);
}
pthread_join(thread, NULL);
return 0;
}
```
在这个示例中,我们创建了一个名为 `thread_function` 的线程函数,该函数接受一个参数 `arg`。我们在主函数中创建了一个线程对象,并使用 `pthread_create()` 函数启动线程。线程函数的参数为 `1L`,表示线程 ID。最后,我们使用 `pthread_join()` 函数等待线程结束。