· 6 years ago · Nov 22, 2018, 04:46 PM
1#include <stdlib.h>
2#include <stdio.h>
3#include <pthread.h>
4
5#define P 1
6#define C 2
7#define BUF 10
8
9FILE *mf;
10
11void *f(void *args) {
12 int id = (int) args;
13 char a[16];
14 while(fscanf(mf,"%s",a)!=EOF)
15 {
16 printf("my id:%d a=%s \r\n",id,a);
17 pthread_yield(NULL);
18 }
19
20
21 pthread_exit(0);
22}
23
24int main() {
25 pthread_t thread1;
26 pthread_t thread2;
27 pthread_t thread3;
28
29 mf=fopen("test2.txt","r");
30 if (mf == NULL) printf ("error");
31 int id[3]={1,2,3};
32 pthread_create(&thread1, NULL, f, &id[0]);
33 pthread_create(&thread2, NULL, f, &id[1]);
34 pthread_create(&thread3, NULL, f, &id[2]);
35
36 pthread_join(thread1, NULL);
37 pthread_join(thread2, NULL);
38 pthread_join(thread3, NULL);
39 return 0;
40}
41
42192.168.1.10
43192.168.1.11
44192.168.1.12
45192.168.1.13
46192.168.1.14
47192.168.1.15
48192.168.1.16
49192.168.1.17
50192.168.1.18
51192.168.1.19
52192.168.1.20
53192.168.1.21
54192.168.1.22