質問編集履歴

3

メモリの解放について改善

2016/12/23 04:28

投稿

johejo
johejo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -212,160 +212,166 @@
212
212
 
213
213
  thread_id = ((struct ThreadArgs *) threadArgs) -> thread_id;
214
214
 
215
+
216
+
215
-
217
+ while ( *sum < *count_num) {
218
+
219
+ (*n)++;
220
+
221
+ printf("Thread%d %d\n", thread_id, *n);
222
+
223
+ for (i = 0; i < 50000000; i++) {}
224
+
225
+ }
226
+
227
+
216
228
 
217
229
  free(threadArgs);
218
230
 
219
-
231
+ }
232
+
233
+
234
+
220
-
235
+ int main(int argc, char *argv[])
236
+
237
+ {
238
+
239
+ int i, j, *n, *top, count_num, thread_num, sum;
240
+
241
+ clock_t time1, time0;
242
+
243
+ double diff;
244
+
245
+ struct ThreadArgs *threadArgs;
246
+
247
+ pthread_t mythread;
248
+
249
+ int thread_id;
250
+
251
+
252
+
253
+ if (argc != 3) {
254
+
255
+ exit(1);
256
+
257
+ }
258
+
259
+
260
+
261
+ count_num = atoi(argv[1]);
262
+
221
- while ( *sum < *count_num) {
263
+ thread_num = atoi(argv[2]);
222
-
223
- (*n)++;
264
+
224
-
265
+
266
+
225
- printf("Thread%d %d\n", thread_id, *n);
267
+ printf("begin %d %d\n", count_num, thread_num);
268
+
269
+
270
+
226
-
271
+ n = (int *)malloc(sizeof(int) * thread_num);
272
+
273
+
274
+
275
+ top = n;
276
+
277
+
278
+
227
- for (i = 0; i < 50000000; i++) {}
279
+ for (i = 0, n = top; i < thread_num; i++, n++) {
280
+
281
+ *n = 0;
228
282
 
229
283
  }
230
284
 
285
+
286
+
287
+ sum = 0;
288
+
289
+
290
+
291
+ time0 = clock();
292
+
293
+
294
+
295
+ thread_id = 0;
296
+
297
+
298
+
299
+ for (i = 0, n = top; i < thread_num; i++, n++) {
300
+
301
+ if((threadArgs = (struct ThreadArgs *)malloc(sizeof(struct ThreadArgs))) == NULL){
302
+
303
+ fprintf(stderr, "malloc failed¥n"), exit(1);
304
+
305
+ }
306
+
307
+
308
+
309
+ threadArgs -> count_num = &count_num;
310
+
311
+ threadArgs -> n = n;
312
+
313
+ threadArgs -> sum = &sum;
314
+
315
+ threadArgs -> thread_id = thread_id;
316
+
317
+
318
+
319
+ if(pthread_create(&mythread, NULL, thread_func, (void *) threadArgs) != 0){
320
+
321
+ fprintf(stderr, "pthread_create() failed¥n"), exit(1);
322
+
323
+ }
324
+
325
+
326
+
327
+ thread_id++;
328
+
329
+ }
330
+
331
+
332
+
333
+ while (sum < count_num) {
334
+
335
+ pthread_mutex_lock(&MyMutex);
336
+
337
+ for (i = 0, sum = 0, n = top; i < thread_num; i++, n++) {
338
+
339
+ sum += *n;
340
+
341
+ }
342
+
343
+ pthread_mutex_unlock(&MyMutex);
344
+
345
+ //printf("%d %d\n", sum, count_num);
346
+
347
+ }
348
+
349
+ printf("sum %d\n", sum);
350
+
351
+
352
+
353
+ time1 = clock();
354
+
355
+
356
+
357
+ diff =(double)(time1 - time0) / CLOCKS_PER_SEC;
358
+
359
+
360
+
361
+ printf("%.3f[sec]\n", diff);
362
+
363
+
364
+
365
+ free(top);
366
+
367
+
368
+
369
+ return 0;
370
+
231
371
  }
232
372
 
233
373
 
234
374
 
235
- int main(int argc, char *argv[])
236
-
237
- {
238
-
239
- int i, j, *n, *top, count_num, thread_num, sum;
240
-
241
- clock_t time1, time0;
242
-
243
- double diff;
244
-
245
- struct ThreadArgs *threadArgs;
246
-
247
- pthread_t mythread;
248
-
249
- int thread_id;
250
-
251
-
252
-
253
- if (argc != 3) {
254
-
255
- exit(1);
256
-
257
- }
258
-
259
-
260
-
261
- count_num = atoi(argv[1]);
262
-
263
- thread_num = atoi(argv[2]);
264
-
265
-
266
-
267
- printf("begin %d %d\n", count_num, thread_num);
268
-
269
-
270
-
271
- n = (int *)malloc(sizeof(int) * thread_num);
272
-
273
-
274
-
275
- top = n;
276
-
277
-
278
-
279
- for (i = 0, n = top; i < thread_num; i++, n++) {
280
-
281
- *n = 0;
282
-
283
- }
284
-
285
-
286
-
287
- sum = 0;
288
-
289
-
290
-
291
- time0 = clock();
292
-
293
-
294
-
295
- thread_id = 0;
296
-
297
-
298
-
299
- for (i = 0, n = top; i < thread_num; i++, n++) {
300
-
301
- if((threadArgs = (struct ThreadArgs *)malloc(sizeof(struct ThreadArgs))) == NULL){
302
-
303
- fprintf(stderr, "malloc failed¥n"), exit(1);
304
-
305
- }
306
-
307
-
308
-
309
- threadArgs -> count_num = &count_num;
310
-
311
- threadArgs -> n = n;
312
-
313
- threadArgs -> sum = &sum;
314
-
315
- threadArgs -> thread_id = thread_id;
316
-
317
-
318
-
319
- if(pthread_create(&mythread, NULL, thread_func, (void *) threadArgs) != 0){
320
-
321
- fprintf(stderr, "pthread_create() failed¥n"), exit(1);
322
-
323
- }
324
-
325
-
326
-
327
- thread_id++;
328
-
329
- }
330
-
331
-
332
-
333
- while (sum < count_num) {
334
-
335
- pthread_mutex_lock(&MyMutex);
336
-
337
- for (i = 0, sum = 0, n = top; i < thread_num; i++, n++) {
338
-
339
- sum += *n;
340
-
341
- }
342
-
343
- pthread_mutex_unlock(&MyMutex);
344
-
345
- //printf("%d %d\n", sum, count_num);
346
-
347
- }
348
-
349
- printf("sum %d\n", sum);
350
-
351
-
352
-
353
- time1 = clock();
354
-
355
-
356
-
357
- diff =(double)(time1 - time0) / CLOCKS_PER_SEC;
358
-
359
-
360
-
361
- printf("%.3f[sec]\n", diff);
362
-
363
-
364
-
365
- return 0;
366
-
367
- }
368
-
369
375
 
370
376
 
371
377
  ```

2

ソースコードの誤りの訂正

2016/12/23 04:28

投稿

johejo
johejo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -168,6 +168,14 @@
168
168
 
169
169
 
170
170
 
171
+ static pthread_mutex_t MyMutex=PTHREAD_MUTEX_INITIALIZER;
172
+
173
+
174
+
175
+ void *thread_func(void *threadArgs);
176
+
177
+
178
+
171
179
  struct ThreadArgs
172
180
 
173
181
  {
@@ -184,10 +192,6 @@
184
192
 
185
193
 
186
194
 
187
- void *thread_func(void *threadArgs);
188
-
189
-
190
-
191
195
  void *thread_func(void *threadArgs) {
192
196
 
193
197
  int i, thread_id;

1

排他処理を追加しました。追加質問内容と実行例について加筆しました。

2016/12/23 04:24

投稿

johejo
johejo

スコア12

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,126 @@
32
32
 
33
33
 
34
34
 
35
+ #実行例について
36
+
37
+ スレッド数4で実行した場合
38
+
39
+ $./a.out 100 4
40
+
41
+ begin 100 4
42
+
43
+ Thread0 1
44
+
45
+ Thread1 1
46
+
47
+ Thread2 1
48
+
49
+ Thread3 1
50
+
51
+ Thread3 2
52
+
53
+ Thread1 2
54
+
55
+ Thread2 2
56
+
57
+ Thread0 2
58
+
59
+ Thread1 3
60
+
61
+ Thread3 3
62
+
63
+ Thread2 3
64
+
65
+ Thread0 3
66
+
67
+ ===中略===
68
+
69
+ Thread3 24
70
+
71
+ Thread1 24
72
+
73
+ Thread2 24
74
+
75
+ Thread0 24
76
+
77
+ Thread3 25
78
+
79
+ Thread1 25
80
+
81
+ Thread0 25
82
+
83
+ Thread2 25
84
+
85
+ sum 100
86
+
87
+ 12.109[sec]
88
+
89
+
90
+
91
+ スレッド数5で実行した場合
92
+
93
+ $./a.out 100 5
94
+
95
+ begin 100 5
96
+
97
+ Thread0 1
98
+
99
+ Thread1 1
100
+
101
+ Thread2 1
102
+
103
+ Thread3 1
104
+
105
+ Thread1 2
106
+
107
+ Thread3 2
108
+
109
+ Thread0 2
110
+
111
+ Thread2 2
112
+
113
+ Thread1 3
114
+
115
+ Thread3 3
116
+
117
+ Thread0 3
118
+
119
+ Thread2 3
120
+
121
+ ===中略===
122
+
123
+ Thread1 95
124
+
125
+ Thread3 95
126
+
127
+ Thread2 96
128
+
129
+ Thread0 97
130
+
131
+ Thread1 96
132
+
133
+ Thread3 96
134
+
135
+ Thread2 97
136
+
137
+ Thread0 98
138
+
139
+ Thread1 97
140
+
141
+ Thread4 1
142
+
143
+ Thread3 97
144
+
145
+ Thread2 98
146
+
147
+ sum 391
148
+
149
+ 38.390[sec]
150
+
151
+
152
+
153
+
154
+
35
155
  ###該当のソースコード
36
156
 
37
157
  ```C
@@ -208,12 +328,16 @@
208
328
 
209
329
  while (sum < count_num) {
210
330
 
331
+ pthread_mutex_lock(&MyMutex);
332
+
211
333
  for (i = 0, sum = 0, n = top; i < thread_num; i++, n++) {
212
334
 
213
335
  sum += *n;
214
336
 
215
337
  }
216
338
 
339
+ pthread_mutex_unlock(&MyMutex);
340
+
217
341
  //printf("%d %d\n", sum, count_num);
218
342
 
219
343
  }
@@ -244,6 +368,16 @@
244
368
 
245
369
 
246
370
 
371
+ ##追加の質問内容
372
+
373
+ なぜスレッド数5の実行例では、Thread 4(5つめのスレッド)がすぐに開始されないのでしょうか。
374
+
375
+ ##追記
376
+
377
+ 排他処理を追加しました。
378
+
379
+ 追加の質問内容と実行例について加筆しました。
380
+
247
381
 
248
382
 
249
383
  ###補足情報(言語/FW/ツール等のバージョンなど)