Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Not able to generate race condition

未关闭
#142 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
c

调研方向

首先编译并运行所提供的 pthread 程序,然后检查其打印出的计数器值和最终检查值。审查共享更新的行为以及缺少同步的情况;完成标准是提供基于观察到的输出的解释,并确定该示例展示了什么。

由索引模型根据 Issue 内容生成。

描述

I wrote a code using pthreads where multiple threads were accessing and modifying a global variable and race condition should occur without any locking mechanism or atomic operations but it was not the case. Can anyone explain why this did not occur?
The code is as follows:

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

#define NTHREADS 150
  
pthread_t tid[NTHREADS]; 
int counter; 
pthread_mutex_t lock; 
int check = 3;
  
void* trythis(void *arg) 
{ 
    unsigned long i = 0; 
    // pthread_mutex_lock(&lock);
    counter += 1; 
    // pthread_mutex_unlock(&lock);
    printf("\n Job %d has started\n", counter); 
    check+=1;
    printf("\n Job %d has finished\n", counter); 
     check+=1;
  
    return NULL; 
} 
  
int main(void) 
{ 
    int i = 0; 
    int error; 
  
    if (pthread_mutex_init(&lock, NULL) != 0) 
    { 
        printf("\n mutex init has failed\n"); 
        return 1; 
    } 
  
    while(i < NTHREADS) 
    { 
        error = pthread_create(&(tid[i]), NULL, &trythis, NULL); 
        if (error != 0) 
            printf("\nThread can't be created :[%s]", strerror(error)); 
        i++; 
    } 
  
    for(i=0;i<NTHREADS;i++){
      pthread_join(tid[i],NULL);
    } 
    pthread_mutex_destroy(&lock);
    printf("%d\n",check ); 
  
    return 0; 
} 
主要语言
WebAssembly
星标
769
派生
54
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

WebAssembly/threads 的其他 Issue

查看 WebAssembly/threads 的全部 Issue

相似的 Issue

更多 Operating Systems Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。