Created
June 8, 2013 02:22
-
-
Save whosaysni/5733681 to your computer and use it in GitHub Desktop.
hello and farewell pthread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
pthtest --- hello pthread. | |
gcc -pthread -o pthtest pthtest.c | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
typedef struct data_{ | |
int vala; | |
int valb; | |
} data ; | |
void *thread_a( void *dat); | |
void *thread_b( void *dat); | |
int main(int argc, char** argv) { | |
int ret; | |
data datX; | |
pthread_t threadA, threadB; | |
pthread_attr_t attr; | |
datX.vala = 0; | |
datX.valb = 0; | |
pthread_create (&threadA, NULL, thread_a, (void*)&datX ); | |
pthread_create (&threadB, NULL, thread_b, (void*)&datX ); | |
while(1){ | |
fprintf(stderr, "\ra(id %d) is %18d, b(id %d) is %18d", | |
threadA, datX.vala, threadB, datX.valb); | |
fflush(stderr); | |
usleep(250); | |
} | |
exit(0); | |
} | |
void *thread_a(void *dat){ | |
data* ptr; | |
ptr = (data*)dat; | |
while(1){ | |
ptr-> vala = ptr->vala +1; | |
} | |
return; | |
} | |
void *thread_b(void *dat){ | |
data* ptr; | |
ptr = (data*)dat; | |
while(1){ | |
ptr-> valb = ptr->valb +2; | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment