[ / / / / / / / / / / / / / ] [ dir / hkon9 / ita / jewess / lewd / sapphic ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Email
Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

File (hide): b8023773d428230⋯.jpg (28.25 KB, 1000x1157, 1000:1157, stainless-serving-fork.jpg) (h) (u)

[–]

 No.1020490>>1020497 >>1020516 >>1020554 [Watch Thread][Show All Posts]

I have a question about threads in Linux.

Say i have a global variable "int gVar" and i have a thread that uses it too. does the thread always share the same gloal variable? or does the thread get it's own instance of the variable?

This could cause problems for me because im writing a network program and it needs to accept new connections in a new thread. But if each thread is sharing the same variables that will cause a massive fuck up.

 No.1020497>>1020537

>>1020490 (OP)

You mean inside the program you are using multi-threading or you are forking? Because I'm almost certain forks don't share 'global' variables other than env.


 No.1020498

Depends principally on your programming language and threading library. Likely shared across threads (not processes). The documentation for your language will be a better help than anyone here, closely followed by a search engine. Also >>733048


 No.1020499>>1020505

I assume you're trying to describe this (I'm assuming you're using pthreads)?

int gVar = 10;
int main() {...}
void *some_thread(void *data_ptr) {
int lVar = gVar;
lVar++;
printf("%d\n", lVar); // prints 11
printf("%d\n", gVar); // prints 10
}

Value of gVar can be modified if gVar is a pointer.

int *gVar;
int main() {
gVar = malloc(sizeof(int));
*gVar = 10; ...}
void *some_thread(void *data_ptr) {
int *lVar = gVar;
*lVar = lVar + 1;
printf("%d\n", *lVar); // prints 11
printf("%d\n", *gVar); // prints 11
}


 No.1020502

Forgot to dereference lVar in addition expression.

>*lVar = lVar + 1;

should be:

>*lVar = *lVar + 1;


 No.1020505>>1020507

>>1020499

You need to use mutex or atomic variables if you want to be correct, right?


 No.1020507

>>1020505

Yes, that is if you want to modify the global variable. I'm still not entirely sure what OP wants to do or what the problem actually is.


 No.1020516

>>1020490 (OP)

This should probably find its way to the sticky, but global variables are shared across threads, yes. There is no way that I'm aware of to get around this, you'll have to use local variables instead.


 No.1020532

Yes, by default global variables are shared between threads. If you want each thread to have its own copy, you can make it thread-local.

In C++11:

thread_local int gVar;
In C11:
_Thread_local int gVar;


 No.1020537>>1020552 >>1020555


 No.1020552

>>1020537

If you're creating an entire process rather than just a thread, then obviously the global variables are not going to be shared.


 No.1020554

>>1020490 (OP)

This is why mutexes and locks exist anon.


 No.1020555>>1020556

>>1020537

Also, you should read the link you just posted:

>fork() creates a new process by duplicating the calling process.

>The child process and the parent process run in separate memory spaces.

Couldn't be clearer.


 No.1020556

>>1020555

i knew forking wouldn't cause problems. i made this thread because im trying to decide between threading and forking. i guess forking is just as good.




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 5
13 replies | 0 images | Page ?
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / hkon9 / ita / jewess / lewd / sapphic ][ watchlist ]