首页 > 其他分享 >多线程

多线程

时间:2023-10-18 17:34:15浏览次数:21  
标签:NULL thread num mutex pthread gMutex 多线程

#include <stdio.h>
#include <pthread.h>
#include <Windows.h>
static int g_num = 0;
static int g_c = 0;
pthread_mutex_t gMutex_num = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t gMutex_c = PTHREAD_MUTEX_INITIALIZER;

void* test(void *para)
{
	int a;
	for (int i = 0; i < 1; i++) {
		pthread_mutex_lock(&gMutex_num);
		printf("pid = %d, g_num = %d\n",para, g_num++);
		a = g_num;
		pthread_mutex_unlock(&gMutex_num);
		
		pthread_mutex_lock(&gMutex_c);
		printf("pid = %d, g_c + a = %d\n",para, g_c + a);
		pthread_mutex_unlock(&gMutex_c);
		
	}
}

void* test1(void *para)
{
	int b;
	for (int i = 0; i < 1; i++) {
		pthread_mutex_lock(&gMutex_c);
		printf("pid = %d, g_c = %d\n",para, g_c++);
		b = g_c;
		pthread_mutex_unlock(&gMutex_c);
		
		pthread_mutex_lock(&gMutex_num);
		printf("pid = %d, g_num + b = %d\n",para, g_num + b);
		pthread_mutex_unlock(&gMutex_num);
		
	}
}


int main()
{
	pthread_t thread_a;
	pthread_create(&thread_a, NULL, test, (void *)1);	
	pthread_t thread_b;
	pthread_create(&thread_b, NULL, test, (void *)2);
//	pthread_t thread_c;
//	pthread_create(&thread_c, NULL, test1, (void *)3);	
//	pthread_t thread_d;
//	pthread_create(&thread_d, NULL, test1, (void *)4);
//	
	pthread_join(thread_a, NULL);
	pthread_join(thread_b, NULL);
//	pthread_join(thread_c, NULL);
//	pthread_join(thread_d, NULL);
	
	printf("\ng_num = %d g_c = %d\n", g_num, g_c);
	return 0;
}

标签:NULL,thread,num,mutex,pthread,gMutex,多线程
From: https://www.cnblogs.com/zhouhongyuan/p/17772933.html

相关文章

  • 【玩转Python系列【小白必看】Python多线程爬虫:下载表情包网站的图片
    前言本文主要介绍了使用Python编写的多线程爬虫程序,用于下载表情包网站上的图片。通过解析网页内容和使用XPath定位,可以获取到图片的URL,并将其保存到本地。1.导入模块和库importrequestsfromlxmlimportetreefromthreadingimportThreadfromqueueimportQueueim......
  • 多线程编程同步:读写锁
    读写锁的定义互斥锁锁住后,保证仅有一个线程处理数据(多线程共享的)。要是数据的读取比写入更频繁,且读取操作不涉及共享变量的修改,应允许多个线程读取操作对共享变量的读取。直接使用互斥锁效率太低,若使用读写锁,可以大大提高效率。读写锁的分配规则:1)只要没有线程持有某个特定的读......
  • 在Matplotlib中使用多线程multiprocessing举例
    在Matplotlib中使用多线程Matplotlib提供了一些机制来支持多线程的使用,比如使用matplotlib.pyplot.switch_backend()方法指定可用的图形后端或使用matplotlib.figure.Figure对象的canvas属性来实现绘图。但是,这些机制都需要特别小心地管理和控制,否则会引发线程之间的数据竞争和访......
  • 使用Guava的ListenableFuture完成异步多线程任务并返回结果
    privatestaticExecutorServiceexecutors=newThreadPoolExecutor(5,20,0L,TimeUnit.MILLISECONDS,newLinkedBlockingQueue<Runnable>(10),newThreadFactoryBuilder().setNameFormat("抓数据线程-%d").build());publicstaticvoidmain(String[]arg......
  • java实现大文件多线程上传案例
    当机器内存大小为4G,需要上传一个大小为50G的文件时,为了避免内存溢出,可以采用分片上传的方式,即将大文件切分成多个小片段进行并发上传。以下是一个详细的方案和代码实现示例:方案说明:将大文件切分成多个大小适当的片段(例如每个片段大小为100MB)。创建一个线程池来管理并发上传任务,......
  • Qt/C++编写物联网组件/支持modbus/rtu/tcp/udp/websocket/mqtt/多线程采集
    一、功能特点支持多种协议,包括Modbus_Rtu_Com/Modbus_Rtu_Tcp/Modbus_Rtu_Udp/Modbus_Rtu_Web/Modbus_Tcp/Modbus_Udp/Modbus_Web等,其中web指websocket。支持多种采集通讯方式,包括串口和网络等,可自由拓展其他方式。自定义采集间隔(精确到毫秒)和超时次数,超时后自动将离线的文件......
  • 多线程编程同步:互斥锁和条件变量
    多线程同步怎样同步多个线程或多个进程的活动?为允许在线程或进程间共享数据,同步通常是必需的。而互斥锁和条件变量是同步的基本组成部分。互斥锁用于保护临界区(criticalregion),以保证任何时刻只有一个线程在执行其中的代码,或者任何时刻只有一个进程在执行其中的代码。互斥......
  • 经典多线程题目
    1.三种线程按顺序执行publicclassTest1{//privatestaticLoggerlog=Logger.getLogger(Test2.class);publicstaticvoidmain(String[]args)throwsInterruptedException{//创建三个线程按照线程a,b,c执行Threada=newPrintThread()......
  • 锁+多线程
     互斥锁mutex:保证共享数据操作的完整性,保证在任一时刻只能有一个线程访问对象。锁有两个操作。一个P操作(上锁),一个V操作(解锁)。P和V都是原子操作,就是在执行P和V操作时,不会被插队。锁一般使用信号量来实现的,mutex其实就是信号量=1。互斥量就是同一时间能够分给一个人,即S=1。S=......
  • python多线程with方式加锁
    python多线程with方式加锁"""pythonTreading中的Lock模块提供了加锁和释放锁的方法,分别是acquire()和release().这两个方法可以搭配python的with语句使用."""#示例fromthreadingimportLock​temp_lock=Lock()​withtemp_lock: print(temp_lock) #输出是<locked......