Thread View: pl.comp.lang.c
3 messages
3 total messages
Started by heby
Wed, 28 Oct 2020 10:30
Dostęp do innej zmiennej w zależnosci od wątku
Author: heby
Date: Wed, 28 Oct 2020 10:30
Date: Wed, 28 Oct 2020 10:30
27 lines
1677 bytes
1677 bytes
Cześć. Czy obecnie koszerne jest robienie: thread_local int foo ... czy też może jest jakiś gotowiec w std/boost:: który działa tak: std::thread_unique< int > foo; Thread1: foo = 4; Thread2: foo = 10; Thread1: bar = foo; // bar == 4; Thread2: bar = foo; //bar == 10 Innymi słowy pod spodem jest "mapa" threadid->zmienna i to jest ładnie opakowane? Taki thread local storage, ale emulowany ręcznie. Problem w tym że nie mam w tym konkretnym przypadku kompilatora z supportem dla thread_local...
Re: Dostęp do innej zmiennej w zależnosci od wątku
Author: michal.lyszczek
Date: Wed, 28 Oct 2020 15:15
Date: Wed, 28 Oct 2020 15:15
64 lines
2703 bytes
2703 bytes
On 2020-10-28 11:30:17, heby wrote:
> Cześć.
>
> Czy obecnie koszerne jest robienie:
>
> thread_local int foo
>
> ... czy też może jest jakiś gotowiec w std/boost:: który działa tak:
>
> std::thread_unique< int > foo;
>
> Thread1:
> foo = 4;
>
> Thread2:
> foo = 10;
>
> Thread1:
> bar = foo; // bar == 4;
>
> Thread2:
> bar = foo; //bar == 10
>
> Innymi słowy pod spodem jest "mapa" threadid->zmienna i to jest ładnie
> opakowane? Taki thread local storage, ale emulowany ręcznie.
>
> Problem w tym że nie mam w tym konkretnym przypadku kompilatora z supportem
> dla thread_local...
Jak używasz pthread to możesz użyć pthread_setspecific(3) oraz
pthread_getspecific(3).
W boost wygląda, że jest "boost::thread_specific_ptr".
Jeżeli czysty C to najprościej było by zrobić strukturę z
globalnymi zmiennymi w lokalnej funkcji, coś takiego:
struct globals {
int foo;
int bar;
}
void new_thread() {
struct globals g;
thread_lib_create(thread_function, &g);
}
void thread_function(struct globals *g) {
g->foo = 4;
g->bar = 2;
}
W takim przypadku każdy wątek będzie miał swoje własne lokalne globalsy,
jedyna różnica, że do zmiennych nie odwołujesz się jako "foo" czy "bar",
a przez wskaźnik na strukturę "g->foo", "g->bar".
--
.-----------------.-------------------.---------------------.------------------.
| Michal Lyszczek | Embedded C, Linux | Company Address | .-. open source |
| +48 727 564 419 | Software Engineer | Leszczynskiego 4/29 | oo| supporter |
| https://bofc.pl `----.--------------: 50-078 Wroclaw, Pol | /`'\ & |
| GPG FF1EBFE7E3A974B1 | Bits of Code | NIP: 813 349 58 78 |(\_;/) programer |
`----------------------^--------------^---------------------^------------------'
Re: Dostęp do innej zmiennej w zależnosci od wątku
Author: heby
Date: Wed, 28 Oct 2020 15:18
Date: Wed, 28 Oct 2020 15:18
5 lines
1478 bytes
1478 bytes
On 28/10/2020 16:15, michal.lyszczek@bofc.pl wrote: > Jak używasz pthread to możesz użyć pthread_setspecific(3) oraz > pthread_getspecific(3). O nie nie, przenośnie ma być ;)
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads