c,#include,#includevoid* thread_function(void* arg) {, printf("Hello from new thread!,");, return NULL;,}int main() {, pthread_t thread_id;, pthread_create(&thread_id, NULL, thread_function, NULL);, pthread_join(thread_id, NULL);, return 0;,},
“
在Linux环境下,使用C语言进行多线程编程是一项常见且强大的技术,通过创建多个线程,可以同时执行多个任务,提高程序的并发性和效率,下面将通过一个具体的实例来详细讲解如何在Linux下使用C语言实现多线程编程。
以下是一个通过创建两个线程来实现对一个数进行递加的示例代码:
/* thread_example.c : C multiple thread programming in Linux */ #include <pthread.h> #include <stdio.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread[2]; pthread_mutex_t mut; int number = 0, i; void *thread1() { printf("thread1: I'm thread 1 "); for (i = 0; i < MAX; i++) { printf("thread1: number = %d ", number); pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(2); } printf("thread1: Main function is waiting for me to finish? "); pthread_exit(NULL); } void *thread2() { printf("thread2: I'm thread 2 "); for (i = 0; i < MAX; i++) { printf("thread2: number = %d ", number); pthread_mutex_lock(&mut); number++; pthread_mutex_unlock(&mut); sleep(3); } printf("thread2: Main function is waiting for me to finish? "); pthread_exit(NULL); } void thread_create(void) { int temp; memset(&thread, 0, sizeof(thread)); if ((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) { printf("Failed to create thread 1! "); } else { printf("Thread 1 created "); } if ((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) { printf("Failed to create thread 2! "); } else { printf("Thread 2 created "); } } void thread_wait(void) { if (thread[0] != 0) { pthread_join(thread[0], NULL); printf("Thread 1 has finished "); } if (thread[1] != 0) { pthread_join(thread[1], NULL); printf("Thread 2 has finished "); } } int main() { pthread_mutex_init(&mut, NULL); printf("Main function: I am creating threads, haha "); thread_create(); printf("Main function: I am waiting for the threads to finish their tasks, haha "); thread_wait(); return 0; }
需要使用gcc
编译器来编译上述代码,并链接 pthread 库:
gcc -lpthread -o thread_example thread_example.c
运行生成的可执行文件:
./thread_example
Main function: I am creating threads, haha Thread 1 created Thread 2 created Main function: I am waiting for the threads to finish their tasks, haha thread1: I'm thread 1 thread2: I'm thread 2 thread1: number = 0 thread2: number = 1 thread1: number = 2 thread2: number = 3 thread1: number = 4 thread2: number = 5 thread1: number = 6 thread1: number = 7 thread2: number = 8 thread1: number = 9 thread2: number = 10 thread1: Main function is waiting for me to finish? thread2: Main function is waiting for me to finish? Thread 1 has finished Thread 2 has finished
Q1:为什么需要使用互斥锁(pthread_mutex_t)?
A1:在这个例子中,两个线程都在修改同一个全局变量number
,如果不使用互斥锁来同步对这些共享资源的访问,就会导致竞态条件(race condition),即两个线程可能会同时读取和写入该变量,导致数据不一致或错误的结果,互斥锁确保了在同一时刻只有一个线程能够访问被保护的代码区域(临界区),从而保证了数据的一致性和正确性。
Q2:如何理解线程的创建和等待过程?
A2:线程的创建是通过pthread_create
函数实现的,这个函数接受四个参数:指向线程标识符的指针、线程属性(通常为 NULL 表示默认属性)、要运行的线程函数以及传递给线程函数的参数,在thread_create
函数中,我们创建了两个线程,分别运行thread1
和thread2
函数,线程的等待是通过pthread_join
函数实现的,这个函数会使调用它的主线程等待指定的线程结束,在thread_wait
函数中,我们使用pthread_join
等待两个线程都完成后才继续执行主线程的后续代码,这样可以确保主线程在所有子线程完成任务后才结束程序,同时也能获取子线程的退出状态(如果需要的话)。