首页 > 系统相关 >证明线程确实共享内存

证明线程确实共享内存

时间:2023-03-14 13:24:27浏览次数:47  
标签:共享内存 tptr thread int void 证明 线程 include

实践:

++x

Hello from thread 2
Hello from thread 7
Hello from thread 3
Hello from thread 4
Hello from thread 5
Hello from thread 6
Hello from thread 8
Hello from thread 9
Hello from thread B
Hello from thread A

x++

Hello from thread 1
Hello from thread 2
Hello from thread 3
Hello from thread 4
Hello from thread 5
Hello from thread 6
Hello from thread 7
Hello from thread 8
Hello from thread 9
Hello from thread A

多处理器编程:从入门到放弃 http://jyywiki.cn/OS/2022/slides/3.slides#/1/4

 

入门:thread.h 简化的线程 API

我们为大家封装了超级好用的线程 API (thread.h)

  • create(fn)
    • 创建一个入口函数是 fn 的线程,并立即开始执行
      • void fn(int tid) { ... }
      • 参数 tid 从 1 开始编号
    • 语义:在状态中新增 stack frame 列表并初始化为 fn(tid)
  • join()
    • 等待所有运行线程的 fn 返回
    • 在 main 返回时会自动等待所有线程结束
    • 语义:在有其他线程未执行完时死循环,否则返回
  • 编译时需要增加 -lpthread

 

 

线程库 

thread.h

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdatomic.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>

#define NTHREAD 64
enum { T_FREE = 0, T_LIVE, T_DEAD, };
struct thread {
  int id, status;
  pthread_t thread;
  void (*entry)(int);
};

struct thread tpool[NTHREAD], *tptr = tpool;

void *wrapper(void *arg) {
  struct thread *thread = (struct thread *)arg;
  thread->entry(thread->id);
  return NULL;
}

void create(void *fn) {
  assert(tptr - tpool < NTHREAD);
  *tptr = (struct thread) {
    .id = tptr - tpool + 1,
    .status = T_LIVE,
    .entry = fn,
  };
  pthread_create(&(tptr->thread), NULL, wrapper, tptr);
  ++tptr;
}

void join() {
  for (int i = 0; i < NTHREAD; i++) {
    struct thread *t = &tpool[i];
    if (t->status == T_LIVE) {
      pthread_join(t->thread, NULL);
      t->status = T_DEAD;
    }
  }
}

__attribute__((destructor)) void cleanup() {
  join();
}

  

gcc   -lpthread    
#include "thread.h"
void Ta(){while(1){printf("+++");}}
void Tb(){while(1){printf("___");}}
int main(){
    create(Ta);
    create(Tb);
}
// gcc multiThreaded.c -lpthread && ./a.out

 

 

#include "thread.h"
int x=0;
void Thello(int id){
    usleep(id*1000);
    printf("Hello from  thread %c\n","123456789ABCDEF"[x++]);
}
int main(){
    for(int i=0;i<10;i++){
        create(Thello);
    }
}

 

 

 

标签:共享内存,tptr,thread,int,void,证明,线程,include
From: https://www.cnblogs.com/rsapaper/p/16611262.html

相关文章

  • 为什么要使用线程池?创建线程池的参数有哪些?线程池的原理是什么?
    为什么要使用线程池?线程池有一下优点:线程在是非常宝贵的资源,使用线程池可以重复使用线程,避免频繁的创建和销毁线程所带来的系统损耗。可以根据系统的具体情况调整线程......
  • MFC-多线程
             ......
  • 多线程编程(二)(李慧芹)
    (37条消息)互斥锁(mutex)_清风徐来Groot的博客-CSDN博客Linux中提供一把互斥锁mutex(也称之为互斥量)。每个线程在对资源操作前都尝试先加锁,成功加锁才能操作,操作结束解锁......
  • 【多线程】C++11多线程(简约但不简单) 原创
    【多线程】C++11多线程(简约但不简单) 目录​ ​一、简单使用​​​ ​1、线程参数​​​ ​2.类成员函数做为线程入口​​​ ​3.join:等待线程执......
  • Linux多线程中互斥锁、读写锁、自旋锁、条件变量、信号量详解
    Hello、Hello大家好,我是ST,今天我们继续来聊一聊Linux中多线程编程中的重要知识点,详细谈谈多线程中同步和互斥机制。1、同步和互斥互斥:多线程中互斥是指多个线程访问同一资源......
  • 【线程同步工具】CyclicBarrier源码分析
    在指定状态点同步任务Java并发API提供了可以使多个线程在一个指定点同步的工具类CyclicBarrier,该类前文介绍的CountDownLatch有些类似,但是它的一些特殊性使得其更为......
  • Java线程池
    线程池的目的是通过对线程的管理,让多线程程序中的多线程便捷开发、高效运行。线程池的存在的意义让线程变得可重用,减小线程创建和销毁带来的消耗。线程池中保留的可用......
  • 线程池
    基本原理:    importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;publicclassExecutorsDemo{//创建线程池的两......
  • Linux进程与线程的基本概念及区别
    前言假设你正在玩一款在线多人游戏,在游戏中,有多个角色需要进行不同的操作,例如攻击、移动、释放技能等等。接下来,我们用玩游戏的例子,来解释进程和和线程的概念,以及进程和......
  • 线程状态
       ......