#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
// 线程函数
void *print_hello(void *threadid) {
long tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t = 0; t < NUM_THREADS; t++) {
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, print_hello, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
// 等待所有线程完成
for(t = 0; t < NUM_THREADS; t++) {
pthread_join(threads[t], NULL);
}
printf("All threads completed.\n");
pthread_exit(NULL);
}
pthread.h
用于线程操作NUM_THREADS
为5print_hello
是线程函数,接收一个void指针参数pthread_create
创建新线程pthread_join
等待线程结束pthread_exit
退出#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5
int counter = 0;
pthread_mutex_t mutex;
void *increment_counter(void *threadid) {
long tid = (long)threadid;
pthread_mutex_lock(&mutex);
printf("Thread #%ld working on counter\n", tid);
int temp = counter;
temp++;
counter = temp;
printf("Thread #%ld: counter = %d\n", tid, counter);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t;
// 初始化互斥锁
pthread_mutex_init(&mutex, NULL);
for(t = 0; t < NUM_THREADS; t++) {
rc = pthread_create(&threads[t], NULL, increment_counter, (void *)t);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t = 0; t < NUM_THREADS; t++) {
pthread_join(threads[t], NULL);
}
printf("Final counter value: %d\n", counter);
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
}
counter
和互斥锁mutex
pthread_mutex_init
初始化互斥锁pthread_mutex_lock
和pthread_mutex_unlock
保护临界区pthread_mutex_destroy
销毁互斥锁#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int count = 0;
int in = 0, out = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_producer = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_consumer = PTHREAD_COND_INITIALIZER;
void *producer(void *arg) {
int item;
for(int i = 0; i < 10; i++) {
item = rand() % 100; // 生产一个随机数
pthread_mutex_lock(&mutex);
while(count == BUFFER_SIZE) {
pthread_cond_wait(&cond_producer, &mutex);
}
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
count++;
printf("Produced: %d, count: %d\n", item, count);
pthread_cond_signal(&cond_consumer);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void *consumer(void *arg) {
int item;
for(int i = 0; i < 10; i++) {
pthread_mutex_lock(&mutex);
while(count == 0) {
pthread_cond_wait(&cond_consumer, &mutex);
}
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
printf("Consumed: %d, count: %d\n", item, count);
pthread_cond_signal(&cond_producer);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
int main() {
pthread_t producer_thread, consumer_thread;
pthread_create(&producer_thread, NULL, producer, NULL);
pthread_create(&consumer_thread, NULL, consumer, NULL);
pthread_join(producer_thread, NULL);
pthread_join(consumer_thread, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_producer);
pthread_cond_destroy(&cond_consumer);
return 0;
}
pthread_cond_wait
等待条件变量,pthread_cond_signal
通知等待的线程#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define THREAD_POOL_SIZE 4
#define TASK_QUEUE_SIZE 10
typedef struct {
void (*function)(void *);
void *arg;
} Task;
Task task_queue[TASK_QUEUE_SIZE];
int task_count = 0;
int queue_front = 0, queue_rear = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_t thread_pool[THREAD_POOL_SIZE];
void execute_task(Task *task) {
task->function(task->arg);
}
void *thread_function(void *arg) {
while(1) {
pthread_mutex_lock(&mutex);
while(task_count == 0) {
pthread_cond_wait(&cond, &mutex);
}
Task task = task_queue[queue_front];
queue_front = (queue_front + 1) % TASK_QUEUE_SIZE;
task_count--;
pthread_mutex_unlock(&mutex);
execute_task(&task);
}
return NULL;
}
void submit_task(void (*function)(void *), void *arg) {
pthread_mutex_lock(&mutex);
if(task_count == TASK_QUEUE_SIZE) {
printf("Task queue is full!\n");
pthread_mutex_unlock(&mutex);
return;
}
Task task;
task.function = function;
task.arg = arg;
task_queue[queue_rear] = task;
queue_rear = (queue_rear + 1) % TASK_QUEUE_SIZE;
task_count++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
void sample_task(void *arg) {
int num = *(int *)arg;
printf("Task %d is being processed by thread %lu\n", num, pthread_self());
sleep(1); // 模拟耗时操作
printf("Task %d completed\n", num);
}
int main() {
// 初始化线程池
for(int i = 0; i < THREAD_POOL_SIZE; i++) {
pthread_create(&thread_pool[i], NULL, thread_function, NULL);
}
// 提交任务
for(int i = 0; i < 10; i++) {
int *arg = malloc(sizeof(int));
*arg = i;
submit_task(sample_task, arg);
}
sleep(5); // 等待所有任务完成
// 实际应用中应该有更优雅的退出机制
return 0;
}
要编译这些多线程程序,需要使用-pthread
选项:
gcc program.c -o program -pthread
线程创建失败:
ulimit -a
)数据竞争:
死锁:
性能问题:
内存泄漏:
pthread_join
或pthread_detach
管理线程资源这些示例展示了Linux多线程编程的基本概念和技术,包括线程创建、同步、条件变量和线程池实现。在实际应用中,可以根据需求进行调整和扩展。