zoukankan      html  css  js  c++  java
  • pthread 线程冲突一个简单例子

    从书上看到的,一个很简单的例子,

    当前显示的不会发生冲突的代码,因为对于参数的传递采用值传递,如果改用指针/引用传递,例如如注释部分所表示的,就会产生冲突啦。。。很可爱的一个例子

    例子如下:

    #include <stdio.h>
    #include <pthread.h>
    
    void * print(void *arg){
    //      int id = * (int *)arg;
            int id =  (int )arg;
            printf("thread %d it's sequn is %d\n", (int)pthread_self(), id);
            reutrn NULL;
    }
    
    int main(int argc, char *argv[]){
            const int thread_num = 4;
            int i;
            pthread_t tid[thread_num];
            for(i=0; i<thread_num; i++){
                    pthread_create(&tid[i], NULL, print, (void *)i);
           //       pthread_create(&tid[i], NULL, print, (void *)&i);
            }
            int *ret;
            for(i=0; i<thread_num; i++){
                    pthread_join(tid[i], NULL);
            }
            return 0;
    }
  • 相关阅读:
    资源限制
    垃圾收集器
    GC日志
    happens-before
    maven相互依赖导致无法编译成功
    LVM-逻辑卷常用命令和示意图
    取消RAID5
    扩展RAID5的容量
    模拟RAID5损坏
    创建RAID5
  • 原文地址:https://www.cnblogs.com/superniaoren/p/3060771.html
Copyright © 2011-2022 走看看