当前位置: 首页 > news >正文

C 与 多线程(1)

C 与 多线程(1)

我们可以用C编写多线程程序吗?

  • C 语言标准不支持多线程
  • POSIX Threads (or Pthreads) is a POSIX standard for threads.
  • gcc compiler 提供了一种 pthread 的实现

一个简单的C程序来演示pthread基本功能的使用

请注意,下面的程序只能与带有pthread库的C编译器一起编译。

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. 
#include <pthread.h> 

// A normal C function that is executed as a thread 
// when its name is specified in pthread_create() 
void *myThreadFun(void *vargp) 
{ 
	sleep(1); 
	printf("Printing GeeksQuiz from Thread \n"); 
	return NULL; 
} 

int main() 
{ 
	pthread_t thread_id; 
	printf("Before Thread\n"); 
	pthread_create(&thread_id, NULL, myThreadFun, NULL); 
	pthread_join(thread_id, NULL); 
	printf("After Thread\n"); 
	exit(0); 
}

编译

要使用gcc编译多线程程序,我们需要将其与pthreads库链接。

gcc multithread.c -lpthread

运行

./a.out

多线程具有全局变量和静态变量

所有线程共享数据段全局变量静态变量存储在数据段中。因此,它们被所有线程共享。下面的示例程序进行了演示。

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <pthread.h> 

// Let us create a global variable to change it in threads 
int g = 0; 

// The function to be executed by all threads 
void *myThreadFun(void *vargp) 
{ 
	// Store the value argument passed to this thread 
	int *myid = (int *)vargp; 

	// Let us create a static variable to observe its changes 
	static int s = 0; 

	// Change static and global variables 
	++s; ++g; 

	// Print the argument, static and global variables 
	printf("Thread ID: %d, Static: %d, Global: %d\n", *myid, ++s, ++g); 
} 

int main() 
{ 
	int i; 
	pthread_t tid; 

	// Let us create three threads 
	for (i = 0; i < 3; i++) 
		pthread_create(&tid, NULL, myThreadFun, (void *)&tid); 

	pthread_exit(NULL); 
	return 0; 
} 

请注意,以上是显示线程如何工作的简单示例。在线程中访问全局变量通常不是一个好主意。如果线程2优先于线程1并且线程1需要更改变量,该怎么办。实际上,如果需要多个线程访问全局变量,则应使用互斥锁对其进行访问。

References:

  • http://www.csc.villanova.edu/~mdamian/threads/posixthreads.html
  • Computer Systems : A Programmer

原文:

  • https://www.geeksforgeeks.org/multithreading-c-2/

相关文章:

  • C# 3.0入门(二)
  • 使用weixin-java-miniapp实现微信小程序登录接口
  • Python 下的虚拟环境的使用
  • C#3.0入门系列(三)
  • 一入 Java 深似海 -- S02E02 学习笔记
  • C#3.0入门系列(四)
  • GCC 的简单使用
  • 关于Keil 的一些看法
  • Jackson 简单使用记录
  • java基础设计 开源框架
  • Spring源码学习笔记
  • 正则资料笔记
  • C#接口慨述
  • Inversion of Control 简要笔记
  • 定义接口及定义接口成员
  • ----------
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • Android系统模拟器绘制实现概述
  • CSS 提示工具(Tooltip)
  • ES10 特性的完整指南
  • ES6系统学习----从Apollo Client看解构赋值
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Lucene解析 - 基本概念
  • node-sass 安装卡在 node scripts/install.js 解决办法
  • python_bomb----数据类型总结
  • Spring-boot 启动时碰到的错误
  • 技术发展面试
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端之React实战:创建跨平台的项目架构
  • 如何在 Tornado 中实现 Middleware
  • 问题之ssh中Host key verification failed的解决
  • 小程序开发中的那些坑
  • 小而合理的前端理论:rscss和rsjs
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #QT(串口助手-界面)
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (02)vite环境变量配置
  • (06)金属布线——为半导体注入生命的连接
  • (1)(1.13) SiK无线电高级配置(五)
  • (C++17) std算法之执行策略 execution
  • (delphi11最新学习资料) Object Pascal 学习笔记---第7章第3节(封装和窗体)
  • (done) 两个矩阵 “相似” 是什么意思?
  • (阿里云万网)-域名注册购买实名流程
  • (安卓)跳转应用市场APP详情页的方式
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (四)搭建容器云管理平台笔记—安装ETCD(不使用证书)
  • **PHP分步表单提交思路(分页表单提交)
  • 、写入Shellcode到注册表上线
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .Family_物联网