@unchanted
And how to access them
olli
But it must be mutually exclusive
the goal is to make the access mutual exclusive. Let's assume you have a vector, that is shared across multiple threads, all of them read, remove and add items - how do you make this mutually exclusive?
@unchanted
the goal is to make the access mutual exclusive. Let's assume you have a vector, that is shared across multiple threads, all of them read, remove and add items - how do you make this mutually exclusive?
So that vector must be in a heap memory and then we have to synchronize all the code's critical section to make them access one by one, isn't it?
@unchanted
Heap memory is still present in the system even after the code it executed
olli
that's no reason
olli
int main() { char buffer[1024]; } So you don't need to synchronize the access to buffer ?
Anonymous
Again this depends on system
@unchanted
Other then memory resources what could be other type resources that we can access?
olli
how would you do it w/o exclusive access?
@unchanted
Yes, how can i access network resouces and file system in c/c++?
olli
It's still an exclusive access
olli
to change something you access from multiple threads you need exclusive access
olli
running ++i; on your x86 hardware is not atomic
olli
how should more complex operation be?
Anonymous
I need small help
Anonymous
I want to learn strings and pointer
Anonymous
How to learn that things
@unchanted
How to learn that things
You will read that in operating system course, if you are learning it from scratch...
olli
an example of lockfree would be the CMPXCHG instruction, which makes sure to not access the same memory at the same time on a hardware level. To quote Intel "The CMPXCHG (compare and exchange) and CMPXCHG8B (compare and exchange 8 bytes) instructions are used to synchronize operations in systems that use multiple processors.""
@unchanted
Which cource ya pdf book??
Search understanding OS from scratch in c++
Anonymous
Bro i want for c language
Anonymous
Best easy learning for string and pointer
olli
how do you implement a wait free algorihtm?
olli
yes, atomic operations such as CMPXCHG
olli
which synchronizes on hardware level
olli
no, it is lock free, it is even wait free
olli
and it can be used to implement lock free, wait free and blocking synchronization primitives
olli
yes, and yet you have to use atomic operations to make sure you don't access the same memory at the same time
olli
which was my point, for thread safety the same memory shall not be changed simultaneously - atomic operations synchronize access to it
olli
how do you define concurrent data structures? And yes, changing a memory location needs to happen mutually exclusively - e.g. using the compare and exchange instructions which provide mutually exclusive access on hardware level
olli
well, std::vector is not concurrent anyway
olli
I never said otherwise
olli
however it is difficult and in most cases not fast to make sure threads access different regions. You will most likely never choose wait-free algorithms for speed reasons. I prevents live- and dead-locks however.
@unchanted
Now I am unable to understand on ever what topic you are talking 😅
@unchanted
when are we going to learn all this where are the sources to this embedded knowledge in C++ I mean this level of understanding is amazing and I wish to reach this level
@unchanted
this looks so amazing even in listening to this conversation even a single thing doesn't get in my head😂
@unchanted
keep it up guys you all are amazing
olli
this looks so amazing even in listening to this conversation even a single thing doesn't get in my head😂
e.g. run this code, run it again and compare the results. Try to understand what happens and why. How can you prevent this and make sure the result is deterministic? #include <thread> #include <cstdio> int main() { unsigned long long x = 0; auto fun = [&](){ for (int i = 0; i < 1'000'000; ++i) { ++x; } }; auto th1 = std::thread(fun); auto th2 = std::thread(fun); th1.join(); th2.join(); std::printf("%llu\n", x); }
@unchanted
ok, let me see
olli
what are thread and join doing in this code?
std::thread creates a thread and starts it, join waits for the thread to finish
@unchanted
first outcome: 1087851 second outcome: 1031545 third outcome: 1112568 forth outcome: 1071384 fifth outcome: 1155180
@unchanted
i am unable to get it through these random outcomes
@unchanted
on how thread and join is working
Anonymous
i am unable to get it through these random outcomes
Because there's an undefined behavior
olli
Nice, UB
on purpose :P
Anonymous
on purpose :P
I got it yeah
@unchanted
Pavel
undefined behavior of?
Changing a variable from 2 threads without any synchronization?
@unchanted
oh
Pavel
Also I'm not sure if it's correct to refer to a stack value from other threads, never seen code like this
olli
Also I'm not sure if it's correct to refer to a stack value from other threads, never seen code like this
why? the C++ standards does not mention stack in that sense. And the object is alive for the whole duration of the program
@unchanted
Thankyou bty...
olli
i am unable to get it through these random outcomes
that's basically what can happen if you don't synchronize access from multiple threads. In this case "lost updates" can happen, because both threads read the value (e.g. 0), increment the value (set it to 1) and write it back, so x is 1 and not 2 although both threads executed the increment once. (basically every result between 1E6 and 2E6 is in theory possible) https://en.wikipedia.org/wiki/Read%E2%80%93write_conflict
Sri
Is possible to unlock iphone
Artöm
Is possible to unlock iphone
No once you locked your phone it will be locked forever. You have to buy another one
Artöm
If we reset the ,is it possible?
Resetting is forbidden
Sri
What is this
Anonymous
HI
Anonymous
natural number N (N ≤ 1000) at the input, then a sequence of whole N elements.
Anonymous
#include <iostream> int main() { int N; std::cin >> N; int arr[1000]; for( int i = 0; i <= N; i++){ std::cin >> arr[i]; } for ( int i = 0; i<= N; i++){ std::cout << arr [i] << " "; } }
Anonymous
tell me what should be changed in the code
Alex
you should check N <= 1000
Alex
and traverse in the reverse order the second loop
Artöm
Second for should be reversed
Artöm
you should check N <= 1000
No, it is input precondition
Alex
also change i <= N to i < N
Alex
No, it is input precondition
so you should check it in your code