Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <threads.h>
- int count;
- mtx_t gMutex;
- int fnC1(void *data){
- int i;
- for(i=0;i<5;i++){
- mtx_lock(&gMutex);
- count=i;
- printf("thread %d : = %d\n", (int)data, count);
- mtx_unlock(&gMutex);
- } return 0;
- }
- int fnC2(void *data){
- int i;
- for(i=0;i<5;i++){
- mtx_lock(&gMutex);
- count++;
- printf("thread %d : = %d\n", (int)data, count);
- mtx_unlock(&gMutex);
- } return 0;
- }
- int main(void){
- // mtx_init(&gMutex, mtx_plain);
- int one=1, two=2;
- thrd_t thr1, thr2;
- /* Create two threads */
- if( thrd_create( &thr1, fnC1, (void *)one) != thrd_success)
- printf("Thread creation failed: \n");
- if( thrd_create( &thr2, fnC2, (void *)two) != thrd_success)
- printf("Thread creation failed: \n");
- /* Wait for both threads to finish */
- thrd_join( thr1, NULL);
- thrd_join( thr2, NULL);
- printf ("\n");
- // mtx_destroy(&gMutex);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement