C-Thread-Pool icon indicating copy to clipboard operation
C-Thread-Pool copied to clipboard

My variables is not change

Open huyeeeee opened this issue 6 years ago • 3 comments

the problem is that I defined a struct to store the variable i changed in a foop loop, but after I added all works, then I use function thpool_wait() to wait the work done, but the result is not as expected, the result should be a list of numbers from 0-99, maybe the sequence is variant, but the result of mine is always 99, hope your help,thanks the code `#include "thpool.h"

struct param { int i; }; void subfunc(void *arg) { struct param *p = (struct param *)arg; printf("i = %d\n", p->i); } int main() { threadpool tp = thpool_init(4); int i = 0; struct param p; for (i = 0; i < 100; i++) { p.i = i; thpool_add_work(tp, subfunc, (void *)&p); } thpool_wait(tp); thpool_destroy(tp); }`

the result as follow ~/Code/1010$ ./test.o i = 3 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99

huyeeeee avatar Nov 18 '19 02:11 huyeeeee

Because all threads share one parameter. At last, p->i is 99, so most output is 99.

On Nov 17, 2019, at 18:26, huyeeeee [email protected] wrote:

the problem is that I defined a struct to store the variable i changed in a foop loop, but after I added all works, then I use function thpool_wait() to wait the work done, but the result is not as expected, the result should be a list of numbers from 0-99, maybe the sequence is variant, but the result of mine is always 99, hope your help,thank you `#include "thpool.h"

struct param { int i; }; void subfunc(void *arg) { struct param *p = (struct param *)arg; printf("i = %d\n", p->i); } int main() { threadpool tp = thpool_init(4); int i = 0; struct param p; for (i = 0; i < 100; i++) { p.i = i; thpool_add_work(tp, subfunc, (void *)&p); } thpool_wait(tp); thpool_destroy(tp); }`

the result as follow ~/Code/1010$ ./test.o i = 3 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Pithikos/C-Thread-Pool/issues/80?email_source=notifications&email_token=AHITJ5NRVXKEW36VCQ7HOWLQUH4OXA5CNFSM4JONFWVKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HZ5C3CA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHITJ5IH4OAMUI5WYFEAFSDQUH4OXANCNFSM4JONFWVA.

iamxiaojin86 avatar Nov 18 '19 05:11 iamxiaojin86

Because all threads share one parameter. At last, p->i is 99, so most output is 99. … Thanks for your help! 1.But the first " i " is 3 and there is also a situation that the result formed as some 99s then a different number such as 12 or some other numbers, every time the result is different. 2.Besides, what should I do to avoid the situation, I want to make every number show one time in this threadpool

huyeeeee avatar Nov 18 '19 06:11 huyeeeee

Thanks for your help! 1.But the first " i " is 3 and there is also a situation that the result formed as some 99s then a different number such as 12 or some other numbers, every time the result is different. 2.Besides, what should I do to avoid the situation, I want to make every number show one time in this threadpool

------------------ 原始邮件 ------------------ 发件人: "iamxiaojin"<[email protected]>; 发送时间: 2019年11月18日(星期一) 中午1:12 收件人: "Pithikos/C-Thread-Pool"<[email protected]>; 抄送: "胡江平"<[email protected]>; "Author"<[email protected]>; 主题: Re: [Pithikos/C-Thread-Pool] My variables is not change (#80)

Because all threads share one parameter. At last, p->i is 99, so most output is 99.

> On Nov 17, 2019, at 18:26, huyeeeee <[email protected]> wrote: > > the problem is that I defined a struct to store the variable i changed in a foop loop, but after I added all works, then I use function thpool_wait() to wait the work done, but the result is not as expected, the result should be a list of numbers from 0-99, maybe the sequence is variant, but the result of mine is always 99, hope your help,thank you > #include "thpool.h" &gt; &gt; struct param &gt; { &gt; int i; &gt; }; &gt; void subfunc(void *arg) &gt; { &gt; struct param *p = (struct param *)arg; &gt; printf("i = %d\n", p-&gt;i); &gt; } &gt; int main() &gt; { &gt; threadpool tp = thpool_init(4); &gt; int i = 0; &gt; struct param p; &gt; for (i = 0; i < 100; i++) &gt; { &gt; p.i = i; &gt; thpool_add_work(tp, subfunc, (void *)&amp;p); &gt; } &gt; thpool_wait(tp); &gt; thpool_destroy(tp); &gt; } > > the result as follow > ~/Code/1010$ ./test.o i = 3 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 i = 99 > > — > You are receiving this because you are subscribed to this thread. > Reply to this email directly, view it on GitHub <" target="_blank" rel="noopener">https://github.com/Pithikos/C-Thread-Pool/issues/80?email_source=notifications&email_token=AHITJ5NRVXKEW36VCQ7HOWLQUH4OXA5CNFSM4JONFWVKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HZ5C3CA>;, or unsubscribe <" target="_blank" rel="noopener">https://github.com/notifications/unsubscribe-auth/AHITJ5IH4OAMUI5WYFEAFSDQUH4OXANCNFSM4JONFWVA&gt;. >

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

huyeeeee avatar Nov 21 '19 09:11 huyeeeee