🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #310269

Re: Dostęp do innej zmiennej w zależnosci od wątku

#310269
From: michal.lyszczek
Date: Wed, 28 Oct 2020 15:15
64 lines
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  |
`----------------------^--------------^---------------------^------------------'
Technical Details
Message-ID: <20201028151541.j3t3sd7oytakg4l4@marchewa.redembedded.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!news-1.dfn.de!news.dfn.de!2.eu.feeder.erje.net!feeder.erje.net!usenet.goja.nl.eu.org!aioe.org!.POSTED.3JPWV+RwCn7hg3EkpWhTCw.user.gioia.aioe.org!not-for-mail
References: <rnbh7q$ek2$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!news-1.dfn.de!news.dfn.de!2.eu.feeder.erje.net!feeder.erje.net!usenet.goja.nl.eu.org!aioe.org!.POSTED.3JPWV+RwCn7hg3EkpWhTCw.user.gioia.aioe.org!not-for-mail
From: michal.lyszczek@bofc.pl
Newsgroups: pl.comp.lang.c
Subject: Re: Dostęp do innej zmiennej w
 zależnosci od wątku
Date: Wed, 28 Oct 2020 16:15:41 +0100
Organization: Aioe.org NNTP Server
Lines: 100
Message-ID: <20201028151541.j3t3sd7oytakg4l4@marchewa.redembedded.pl>
References: <rnbh7q$ek2$1@dont-email.me>
NNTP-Posting-Host: 3JPWV+RwCn7hg3EkpWhTCw.user.gioia.aioe.org
Mime-Version: 1.0
X-Complaints-To: abuse@aioe.org
User-Agent: NeoMutt/20180716
X-Notice: Filtered by postfilter v. 0.9.2
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit