首页 > 其他分享 >实验2 多线程

实验2 多线程

时间:2023-04-28 21:22:08浏览次数:33  
标签:rand 多线程 int void NULL 实验 pthread include

创建一个线程

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>

void* threadFunc(void* arg)
{

printf("In NEW threaad\n");

}

int main()
{

	pthread_t tid;
//create thread function
pthread_create(&tid,NULL,threadFunc,NULL);
//wait result

pthread_join(tid,NULL);

printf("IN main thread\n");
return 0;
}	

在linux中运行多线程需要实验 gcc helloprocess.c -o helloprocess.h -pthread的命令


当我们把等待语句注释了

此时我们的main线程没有等待新创建的线程,直接执行了return ,此时程序已经结束了,新创建的线程的执行语句还没有执行

这里记住:一定要在主线程中等待其他线程先执行完毕,要不然return 时其他的线程将自动截至

共享数据段

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
void* hello(void* args){
for (int i = 0; i < 30000; i++){
printf("hello(%d)\n", rand()%100);
sleep(1);
}
}
void* world(void* args){
for (int i = 0; i < 30000; i++){
printf("world(%d)\n", rand()%100);
sleep(2);
}
}
int main(){
srand(time(NULL));
pthread_t tid,tid2;
// 线程创建函数
pthread_create(&tid, NULL, hello, NULL);
pthread_create(&tid2, NULL, world, NULL);
// 等待指定的线程结束
pthread_join(tid, NULL);
pthread_join(tid2, NULL);
printf("In Main Thread\n");
return 0;
}

我们可以看出我们创建的2个线程是并发执行的

接着我们创建⼀个全局变量,我们观察⼀下两个线程是否可以操作同⼀份全局变量

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
int value=100;
void* hello(void* args){
for (int i = 0; i < 3; i++){
printf("hello(%d)\n",value++);
sleep(1);
}
}
void* world(void* args){
for (int i = 0; i < 3; i++){
printf("world(%d)\n", value++);
sleep(2);
}
}
int main(){
srand(time(NULL));
pthread_t tid,tid2;
// 线程创建函数
pthread_create(&tid, NULL, hello, NULL);
pthread_create(&tid2, NULL, world, NULL);
// 等待指定的线程结束
pthread_join(tid, NULL);
pthread_join(tid2, NULL);
printf("In Main Thread\n");
return 0;
}


这表明多线程共享data段。data来自进程s

蒙特卡洛模拟

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
void calculate_pi(int intervals){
unsigned int seed = time(NULL);
int circle_points = 0;
int square_points = 0;
for (int i = 0; i < intervals * intervals; ++i)
{
double rand_x = (double)rand_r(&seed)/RAND_MAX;
double rand_y = (double)rand_r(&seed)/RAND_MAX;
if ((rand_x * rand_x) + (rand_y * rand_y) <= 1){
circle_points++;
}
square_points++;
}
double pi = (double)(4.0*circle_points)/square_points;
printf("The estimated PI is %lf in %d times\n", pi, intervals * intervals);
}

int main(){
clock_t start,deleta;
double time_used;
double pi;
start = clock();
#pragma omp parallel for num_threads(10)
for (int i = 0; i < 10; i++){
calculate_pi(1000*(i+1));
}
deleta = clock() - start;
printf("The time taken in total: %lf seconds\n", (double)deleta/CLOCKS_PER_SE
return 0;



接下来我们来看⼀下开启多线程花费的时间
我们使用10条线程并发执行来代替前面的10次顺序执行

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdlib.h>
void* calculate_pi(void* args){
unsigned int seed = time(NULL);
int circle_points = 0;
int square_points = 0;
int intervals = *((int*)args);
for (int i = 0; i < intervals * intervals; ++i)
{
double rand_x = (double)rand_r(&seed)/RAND_MAX;
double rand_y = (double)rand_r(&seed)/RAND_MAX;
if ((rand_x * rand_x) + (rand_y * rand_y) <= 1){
circle_points++;
}
square_points++;
}
double pi = (double)(4.0*circle_points)/square_points;
printf("The estimated PI is %lf in %d times\n", pi, intervals * intervals);
pthread_exit(0);
}

int main(){
clock_t start,deleta;
double time_used;
start = clock();
pthread_t calculate_pi_threads[10];
int args[10];
for (int i = 0; i < 10; ++i){
args[i] = 1000*(i+1);
pthread_create(calculate_pi_threads+i,NULL,calculate_pi,args+i);
}
for (int i = 0; i < 10; ++i){
pthread_join(calculate_pi_threads[i],NULL);
}
deleta = clock() - start;
printf("The time taken in total: %lf seconds\n", (double)deleta/CLOCKS_PER_SE
return 0;
}

标签:rand,多线程,int,void,NULL,实验,pthread,include
From: https://www.cnblogs.com/swtaa/p/17361593.html

相关文章

  • ubuntu20.04实验用docker环境搭建
    dockerpullubuntu:20.04dockerrun--it--net=host--privileged=trueubuntu:20.04/bin/bashaptupdateapt速度慢可以换源:(6条消息)docker中ubuntu容器更换apt源_NotJc的博客-CSDN博客下载常用软件:apt-getinstallvimapt-getinstallgitgit提速:gitclone提速-Haowe......
  • Java多线程之---用 CountDownLatch 说明 AQS 的实现原理
    本文基于jdk1.8。CountDownLatch的使用前面的文章中说到了volatile以及用volatile来实现自旋锁,例如java.util.concurrent.atomic包下的工具类。但是volatile的使用场景毕竟有限,很多的情况下并不是适用,这个时候就需要synchronized或者各种锁实现了。今天就来说一下几......
  • C# 多线程
    首先要关注电脑配置是否是多核多CPU的。因为一个CPU在同一时刻只能运行一个线程,但是多个CPU在同一时刻就可以运行多个线程。 多线程的优点:1、可以同时完成多个任务;2、可以使程序的响应速度更快;3、可以让占用大量处理时间的任务或当前没有进行处理的任务定期将处理时间让给......
  • 多线程读写文件
    参考:实践1-2:多线程读写文件-l.w.x-博客园(cnblogs.com)得到的结论是,可以多线程读写,但是会有多种情况:多线程同时读同一个文件,在这种情况下并不会造成冲突多线程同时写同一个文件,会造成写数据丢失多线程同时对同一个文件进行写和读,会造成脏读解决办法是加锁,同时......
  • 《Effective C#》系列之(六)——提高多线程的性能
    一、综述《EffectiveC#》中提高多线程性能的方法主要有以下几点:避免锁竞争:锁的使用会导致线程阻塞,从而影响程序的性能。为了避免锁竞争,可以采用无锁编程技术,如CAS(Compare-And-Swap),Interlocked等。使用ThreadPool:ThreadPool是.NETFramework提供的一个线程池,它可以......
  • 202308-啊对对队 实验四:软件开发案例(1)
    202308-啊对对队实验四:软件开发案例(1)项目内容班级博客链接2023春软件工程2020级计算机科学与技术本次作业要求链接实验四:软件开发案例(1))团队名称啊对对队团队课程学习目标(1)软件开发环境部署;(2)练习mysql数据库创建和连接访问技术;(3)掌握数据库应用程序开发技......
  • 火山引擎 DataTester 智能发布平台:智能化 A/B 实验,助力产品快速迭代
    更多技术交流、求职机会,欢迎关注字节跳动数据平台微信公众号,回复【1】进入官方交流群在互联网竞争炙热的红海时代,精益开发高效迭代越来越成为成为产品竞争的利器。产品迭代过程中,如何保障高效的功能迭代安全上线,如何快速实现不同人群的精细化运营,成为了产研人员的新挑战,为了帮......
  • 河北电信“天翼云开放实验室”在雄安揭牌,加速算力资源落地!
    近日,“2023年河北电信天翼云生态合作论坛”在雄安新区成功举办。作为“2023年河北电信数字科技生态大会”的分论坛之一,此次论坛汇集了众多合作伙伴,共同分享了中国电信在云改数转过程中的核心技术与数字化能力成果。论坛上,河北电信“天翼云开放实验室”成功揭牌,标志着天翼云技术融......
  • [Unity]AssetBundle资源更新以及多线程下载
    前言此文章适合不太了解资源加载的萌新,有了入门基础之后再去github上搜大牛写的专业的资源加载方案才能得心应手,不然的话会看的很吃力或者说一脸懵逼。Unity里面关于资源加载我们都知道是下载更新AssetBundle,关于AssetBundle我之前的文章已经详细介绍过,没看过的朋友可以在看一下。......
  • 河北电信“天翼云开放实验室”在雄安揭牌,加速算力资源落地!
    近日,“2023年河北电信天翼云生态合作论坛”在雄安新区成功举办。作为“2023年河北电信数字科技生态大会”的分论坛之一,此次论坛汇集了众多合作伙伴,共同分享了中国电信在云改数转过程中的核心技术与数字化能力成果。论坛上,河北电信“天翼云开放实验室”成功揭牌,标志着天翼云技术融合......