linux多线程以及互斥锁例子

合集下载

linux下C语言多线程编程实例

linux下C语言多线程编程实例
互斥锁相关
互斥锁用来保证一段时间内只有一个线程在执行一段代码。
一 pthread_mutex_init
函数 pthread_mutex_init 用来生成一个互斥锁。NULL 参数表明使用默认属性。如果需要声明特 定属性的互斥锁,须调用函数 pthread_mutexattr_init。函数 pthread_mutexattr_setpshared 和函数 pthread_mutexattr_settype 用来设置互斥锁属性。前一个函数设置属性 pshared,它有 两个取值, PTHREAD_PROCESS_PRIVATE 和 PTHREAD_PROCESS_SHARED。前者用来不同进程中的线 程同步,后者用于同步本进程的不同线程。在上面的例子中,我们使用的是默认属性 PTHREAD_PROCESS_ PRIVATE。后者用来设置互斥锁类型,可选的类型有 PTHREAD_MUTEX_NORMAL、 PTHREAD_MUTEX_ERRORCHECK、 PTHREAD_MUTEX_RECURSIVE 和 PTHREAD _MUTEX_DEFAULT。它们分 别定义了不同的上所、解锁机制,一般情况下,选用最后一个默认属性。
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread));
//comment1
/*创建线程*/
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)
下面是我们的代码: /*thread_example.c : c multiple thread programming in linux

linux线程间通信的几种方法

linux线程间通信的几种方法

linux线程间通信的几种方法Linux是一种开源的操作系统,它支持多线程编程,因此线程间通信是非常重要的。

线程间通信是指在多个线程之间传递数据或信息的过程。

在Linux中,有多种方法可以实现线程间通信,本文将介绍其中的几种方法。

1. 信号量信号量是一种用于线程间同步和互斥的机制。

它可以用来控制对共享资源的访问。

在Linux中,信号量是由sem_t类型的变量表示的。

它有三个主要的操作:初始化、P操作和V操作。

初始化操作用于初始化信号量的值。

P操作用于获取信号量,如果信号量的值为0,则线程会被阻塞,直到信号量的值大于0。

V操作用于释放信号量,将信号量的值加1。

下面是一个使用信号量实现线程间通信的例子:```#include <stdio.h>#include <pthread.h>#include <semaphore.h>sem_t sem;void *thread1(void *arg){sem_wait(&sem);printf("Thread 1\n");sem_post(&sem);pthread_exit(NULL);}void *thread2(void *arg){sem_wait(&sem);printf("Thread 2\n");sem_post(&sem);pthread_exit(NULL);}int main(){pthread_t t1, t2;sem_init(&sem, 0, 1);pthread_create(&t1, NULL, thread1, NULL); pthread_create(&t2, NULL, thread2, NULL); pthread_join(t1, NULL);pthread_join(t2, NULL);sem_destroy(&sem);return 0;}```在这个例子中,我们创建了两个线程,它们都需要获取信号量才能执行。

跟我学Linux编程-12-多线程编程-同步

跟我学Linux编程-12-多线程编程-同步

多线程编程-同步在上一章节中,我们通过程序示例,见证了单线程世界中不可能发生的事件(一个数既是奇数又是偶数)在多线程环境中是怎样分分钟发生的,我通过细分程序执行步骤,分析了奇异事件发生的过程,并探明了其原因:一个线程在对全局变量gcnt进行两次判读的过程中,另一个线刚好改变了这个变量的值。

在多线程编程术语中,称这两个线程同时进入了临界区域。

所谓临界区域,是指多线程环境下两个及以上线程同时执行可能会导致冲突的一段代码。

在上一章节的示例中,这几行代码就是一个临界区域:gcnt++;if (gcnt % 2){if (!(gcnt % 2)) printf("[%d] : %d\n", id, gcnt);}冲突之所以会发生,是因为临界区域的代码,通常需要很多个CPU指令周期才能完成,其运行过程随时可能被打断(进行了线程调试),CPU去运行另外的线程,如果这个线程刚好也进入了临界区域,则异常的程序状态极可能会发生。

如果当某个线程进入临界区域,在其退出区域之前,其他的线程无论如何也不能进入该区域,那么冲突就不会发生。

Linux提供了这种保证多线程进入临界区域互斥的机制,这正是本章节所要介绍的内容:线程锁。

我们今天的示例程序还是在上一章节的示例上改进而来的,我们的任务就是使用线程锁,保证“一个数既是奇数又是偶数”的奇异事件在多线程环境下也不发生,代码如下:#include <pthread.h>#include <stdio.h>#include <unistd.h>int gcnt = 0;pthread_mutex_t g_mutex;void *thread_task(void *arg){int id = (int)arg;while (1){pthread_mutex_lock(&g_mutex);gcnt++;if (gcnt % 2)if (!(gcnt % 2)) printf("[%d] : %d\n", id, gcnt);}pthread_mutex_unlock(&g_mutex);usleep(1);}return NULL;}int main(int argc, char *argv[]){pthread_t thr;pthread_mutex_init(&g_mutex, NULL);pthread_create(&thr, NULL, thread_task, (void *)1);pthread_create(&thr, NULL, thread_task, (void *)2);thread_task((void *)0);return 0;}今天的程序相对于上章的代码,改动非常小,只添加了四行,已使用红色加粗标注。

linux pthread 用法

linux pthread 用法

linux pthread 用法Linux pthread(POSIX线程)是一种多线程库,它提供了在Linux系统上创建和管理线程的API。

使用pthread库,可以编写多线程程序,实现并发执行和资源共享。

下面是一些常用的pthread函数和用法:1.pthread_create():用于创建一个新的线程。

它接受一个指向线程属性的指针,一个指向线程函数的指针,以及传递给线程函数的参数。

函数原型为:intpthread_create(pthread_t *thread, const pthread_attr_t *attr, void*(*start_routine) (void *), void *arg);2.pthread_join():用于等待一个线程的结束。

它接受一个指向线程标识符的指针,以及一个指向用于存储线程返回值的指针的指针。

函数原型为:intpthread_join(pthread_t thread, void **retval);3.pthread_self():用于获取当前线程的标识符。

函数原型为:pthread_tpthread_self(void);4.pthread_detach():用于将一个线程从系统中分离出去。

这通常用于在后台运行的任务,不需要手动等待它们完成。

函数原型为:int pthread_detach(pthread_t thread);5.pthread_equal():用于比较两个线程标识符是否相等。

函数原型为:intpthread_equal(pthread_t thread1, pthread_t thread2);6.pthread_mutex_init():用于初始化一个互斥锁。

函数原型为:intpthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 7.pthread_mutex_lock():用于获取一个互斥锁。

linux下的CC++多进程多线程编程实例详解

linux下的CC++多进程多线程编程实例详解

linux下的CC++多进程多线程编程实例详解linux下的C\C++多进程多线程编程实例详解1、多进程编程#include <stdlib.h>#include <sys/types.h>#include <unistd.h>int main(){pid_t child_pid;/* 创建⼀个⼦进程 */child_pid = fork();if(child_pid == 0){printf("child pid\n");exit(0);}else{printf("father pid\n");sleep(60);}return 0;}2、多线程编程#include <stdio.h>#include <pthread.h>struct char_print_params{char character;int count;};void *char_print(void *parameters){struct char_print_params *p = (struct char_print_params *)parameters;int i;for(i = 0; i < p->count; i++){fputc(p->character,stderr);}return NULL;}int main(){pthread_t thread1_id;pthread_t thread2_id;struct char_print_params thread1_args;struct char_print_params thread2_args;thread1_args.character = 'x';thread1_args.count = 3000;pthread_create(&thread1_id, NULL, &char_print, &thread1_args);thread2_args.character = 'o';thread2_args.count = 2000;pthread_create(&thread2_id, NULL, &char_print, &thread2_args);pthread_join(thread1_id, NULL);pthread_join(thread2_id, NULL);return 0;}3、线程同步与互斥1)、互斥pthread_mutex_t mutex;pthread_mutex_init(&mutex, NULL);/*也可以⽤下⾯的⽅式初始化*/pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex);/* 互斥 */thread_flag = value;pthread_mutex_unlock(&mutex);2)、条件变量int thread_flag = 0;pthread_mutex_t mutex;pthread_cond_t thread_flag_cv;\void init_flag(){pthread_mutex_init(&mutex, NULL);pthread_cond_init(&thread_flag_cv, NULL);thread_flag = 0;}void *thread_function(void *thread_flag){while(1){pthread_mutex_lock(&mutex);while(thread_flag != 0 ){pthread_cond_wait(&thread_flag_cv, &mutex);}pthread_mutex_unlock(&mutex);do_work();}return NULL;}void set_thread_flag(int flag_value){pthread_mutex_lock(&mutex);thread_flag = flag_value;pthread_cond_signal(&thread_flag_cv);pthread_mutex_unlock(&mutex);}感谢阅读,希望能帮助到⼤家,谢谢⼤家对本站的⽀持!。

linux pthread_mutex的原理

linux pthread_mutex的原理

linux pthread_mutex的原理摘要:1.引言2.pthread_mutex 的作用3.pthread_mutex 的工作原理4.pthread_mutex 的使用方法5.总结正文:Linux 中的pthread_mutex 是一种互斥锁,主要用于多线程程序中,以防止多个线程同时访问共享资源造成数据混乱。

pthread_mutex 提供了一种机制,使得在同一时刻只有一个线程可以访问共享资源。

pthread_mutex 的工作原理是通过对共享资源进行加锁和解锁来实现的。

当一个线程需要访问共享资源时,首先尝试对pthread_mutex 进行加锁,如果锁已经被其他线程占用,那么当前线程会被阻塞,等待锁被释放。

当锁被释放后,该线程会被唤醒,继续尝试加锁,直到成功为止。

当线程完成对共享资源的访问后,需要释放pthread_mutex,以便其他线程可以访问共享资源。

在Linux 中,pthread_mutex 有多种操作方法。

首先,需要使用pthread_mutex_init 函数初始化一个pthread_mutex,该函数需要传入两个参数,一个是互斥锁的名称,另一个是互斥锁的初始化模式。

其次,使用pthread_mutex_lock 函数尝试对pthread_mutex 加锁,如果加锁失败,函数会返回一个错误码。

然后,使用pthread_mutex_unlock 函数释放pthread_mutex,使得其他线程可以访问共享资源。

最后,使用pthread_mutex_destroy 函数销毁一个pthread_mutex,该函数会等待所有线程释放该互斥锁后才真正销毁。

Linux系统线程创建及同步互斥方法简要说明(供查考)

Linux系统线程创建及同步互斥方法简要说明(供查考)

Linux系统线程创建及同步互斥方法简要说明(供查考)1、.POSIX线程函数的定义在头文件pthread.h中,所有的多线程程序都必须通过使用#include<pthread.h>包含这个头文件2、用gcc编译多线程程序时,必须与pthread函数库连接。

可以使用以下两种方式编译(建议使用第一种)(1)gcc –D_REENTRANT -o 编译后的目标文件名源文件名-lpthread例如:gcc –D_REENTRANT -o pthread_create pthread_create.c -lpthread (执行该编译结果的方式为:./pthread_create)(2)gcc -pthread -o 编译后的文件名源文件名例如:gcc -pthread -o example example.c一、需要用到的函数的用法提示1、创建线程函数pthread_t a_thread; /*声明a_thread变量,用来存放创建的新线程的线程ID(线程标识符)*/int res=pthread_create(&a_thread,NULL,thread_function,NULL);/*创建一个执行函数thread_function的新线程,线程ID存放在变量a_thread */ 2、退出线程函数pthread_exit(NULL);/*那个线程在执行中调用了该方法,那个线程就退出*/创建和退出线程实例3、连接(等待)线程函数int error;int *exitcodeppthread_t tid; /*用来表示一个已经存在的线程*/error=pthread_join(tid,&exitcodep); /*执行该方法的线程将要一直等待,直到tid 表示的线程执行结束,exitcodep 存放线程tid退出时的返回值*/4、返回线程ID的函数pthread_t t/*声明表示线程的变量t */t=pthread_self( ) /*返回调用该方法的线程的线程ID*/5、判断两个线程是否相等的函数(pthread_equal)int pthread_equal(pthread_t t1, pthread_t t2);/*判断线程t1与线程t2是否线程ID相等*/二、线程同步1、使用互斥量同步线程(实现互斥)(1)互斥量的创建和初始化pthread_mutex_t a_mutex=PTHREAD_MUTEX_INITIALIZER/*声明a_mutex为互斥量,并且初始化为PTHREAD_MUTEX_INITIALIZER */ (2)锁定和解除锁定互斥量pthread_mutex_t a_mutex=PTHREAD_MUTEX_INITIALIZER/*声明互斥量a_mutex*/int rc=pthread_mutex_lock(&a_mutex) /*锁定互斥量a_mutex*/ ………………………………/*锁定后的操作*/int rd= pthread_mutex_unlock(&a_mutex) /*解除对互斥量a_mutex的锁定*/例子:利用互斥量来保护一个临界区pthread_mutex_t a_mutex=PTHREAD_MUTEX_INITIALIZER;pthread_mutex_lock(&a_mutex) /*锁定互斥量a_mutex*//*临界区资源*/pthread_mutex_unlock(&a_mutex) /*解除互斥量a_mutex的锁定*/(3)销毁互斥量Int rc=pthread_mutex_destory(&a_mutex) /*销毁互斥量a_mutex*/2、用条件变量同步线程(实现真正的同步)条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。

Linux多线程编程问题

Linux多线程编程问题

Linux 多线程编程问题1重入问题传统的UNIX没有太多考虑线程问题,库函数里过多使用了全局和静态数据,导致严重的线程重入问题。

1.1–D_REENTRANT /-pthread和errno的重入问题。

所先UNIX的系统调用被设计为出错返回-1,把错误码放在errno中(更简单而直接的方法应该是程序直接返回错误码,或者通过几个参数指针来返回)。

由于线程共享所有的数据区,而errno是一个全局的变量,这里产生了最糟糕的线程重入问题。

比如:do {bytes = recv(netfd, recvbuf, buflen, 0);} while (bytes != -1 && errno != EINTR);在上面的处理recv被信号打断的程序里。

如果这时连接被关闭,此时errno应该不等于EINTR,如果别的线程正好设置errno为EINTR,这时程序就可能进入死循环。

其它的错误码处理也可能进入不可预测的分支。

在线程需求刚开始时,很多方面技术和标准(TLS)还不够成熟,所以在为了解决这个重入问题引入了一个解决方案,把errno定义为一个宏:extern int *__errno_location (void);#define errno (*__errno_location())在上面的方案里,访问errno之前先调用__errno_location()函数,线程库提供这个函数,不同线程返回各自errno的地址,从而解决这个重入问题。

在编译时加-D_REENTRANT就是启用上面的宏,避免errno重入。

另外-D_REENTRANT还影响一些stdio的函数。

在较高版本的gcc里,有很多嵌入函数的优化,比如把printf(“Hello\n”);优化为puts(“hello\n”);之类的,有些优化在多线程下有问题。

所以gcc引入了–pthread 参数,这个参数出了-D_REENTRANT外,还校正一些针对多线程的优化。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
pthread_mutex_lock(&mutex);//给 mutex 加锁,这是一条原子操作,不可能出 现两个线程同时执行这个代码
int *a = (int *) arg; printf("thread%d start\n", *a); int i; for (i = 0; i < 10; i++) {
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//初始化了一个 MUTEX 锁
int count = 0;
void *func1(void *arg) {
int *a = (int *) arg; printf("thread%d start\n", *a); int i; for (i = 0; i < 10; i++) {
printf("process end\n"); return 0; }
//最恰当的互斥用法
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <errno.h> #include <unistd.h> #include <string.h>
pthread_join(thr_d2, NULL);
printf("process end\n"); return 0; }
pthread_exit(NULL); }
int main(int arg, char * args[]) {
printf("process start\n"); pthread_t thr_d1, thr_d2; int i[2]; i[0] = 1; i[1] = 2;
pthread_create(&thr_d1, NULL, func1, &i[0]); pthread_create(&thr_d2, NULL, func1, &i[1]); pthread_join(thr_d1, NULL);
printf("process start\n"); pthread_t thr_d1, thr_d2; int i[2]; i[0] = 1; i[1] = 2;
pthread_create(&thr_d1, NULL, func1, &i[0]); pthread_create(&thr_d2, NULL, func1, &i[1]); pthread_join(thr_d1, NULL); pthread_join(thr_d2, NULL);
一旦线程成为可分离线程之后,就不能再使用 pthread_join 了
可分离线程的使用场景 1、主线程不需要等待子线程 2、主线程不关心子线程的返回码
//互斥锁的例子
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//初始化了一个 MUTEX 锁
void *rintf("thread%d is running\n", *a); sleep(1); } printf("thread%d end\n", *a); pthread_mutex_unlock(&mutex);//给 mutex 解锁 pthread_exit(NULL); }
int main(int arg, char * args[]) {
printf("thread%d is running\n", *a); sleep(1); pthread_mutex_lock(&mutex);//给 mutex 加锁,这是一条原子操作,不可 能出现两个线程同时执行这个代码 count++;//这段代码受到保护,永远只有一个线程可以操作 pthread_mutex_unlock(&mutex);//给 mutex 解锁 } printf("thread%d end\n", *a);
相关文档
最新文档