Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- typedef struct _sync_int{
- int v;
- pthread_mutex_t lock;
- }sync_int;
- sync_int n;
- char fname[32];
- void* fun(void* arg){
- int br=*(int*) arg;
- int curr;
- FILE* f;
- f=fopen(fname, "r+");
- while(fscanf(f, "%d", &curr)==1){
- if(curr==br){
- pthread_mutex_lock(&n.lock);
- n.v++;
- pthread_mutex_unlock(&n.lock);
- }
- }
- return (void*)0;
- }
- int main(int argc, char** argv){
- int i;
- int args[10];
- pthread_t pid_a[10];
- n.v=0;
- strcpy(fname, argv[1]);
- pthread_mutex_init(&n.lock, NULL);
- for(i=0;i<10;i++){
- args[i]=i;
- pthread_create(&pid_a[i], NULL, fun, &args[i]);
- }
- for(i=0;i<10;i++)
- pthread_join(pid_a[i], NULL);
- pthread_mutex_destroy(&n.lock);
- printf("%d\n", n.v);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement