Not able to generate race condition
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- c
- Domain
- operating-systems
Research direction
Start by compiling and running the supplied pthread program, then inspect its printed counter values and final check value. Review the behavior of the shared updates and the absence of synchronization; done means providing an explanation grounded in the observed output and identifying what the example demonstrates.
Written by the indexing model from the issue text.
Description
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;
}
- Dominant language
- WebAssembly
- Stars
- 767
- Forks
- 54
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from WebAssembly/threads
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
WebAssembly/threads#254 ·
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
WebAssembly/threads#253 · 6 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
WebAssembly/threads#245 · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
WebAssembly/threads#240 ·
-
Branch renaming Open
Difficulty 1/5 Under an hour Newbie friendliness 20/100
WebAssembly/threads#237 ·
All issues in WebAssembly/threads
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
zephyrproject-rtos/zephyr#120011 ·
-
libc
Difficulty 2/5 1-2 days Newbie friendliness 76/100
llvm/llvm-project#225506 · 1 comment ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
ValveSoftware/Proton#10167 ·
-
next-devel: s390x build fails — chccwdev/vmur/zkey missing from initramfs after s390utils 2.44 split Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
coreos/fedora-coreos-tracker#2228 · 2 comments ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
pop-os/plymouth-theme#43 ·