@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
olli
@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
Anonymous
Again this depends on system
@unchanted
@unchanted
@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...
Anonymous
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.""
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
olli
@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
@unchanted
@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
Anonymous
@unchanted
on how thread and join is working
Anonymous
olli
@unchanted
@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
Anonymous
@unchanted
@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
Anunay
Sri
Artöm
Artöm
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
Alex
also change i <= N to i < N