Igor🇺🇦
Got it . Thank you so much !
max size_t usually https://onlinegdb.com/B187miHZu
Vlad
So -1 is casted to 9 ?
It's casted to 0xFFFFFFFF for 32 bits. Which is 4 billion with smth
Pavel
In multithreaded environment, how do you guys protect yourself from accessing something from a wrong thread? Do you make every peace of your code thread safe? Or how do you deal with it?
What do you mean by "access from wrong thread"? If you REALLY NEED to access every single peace of code from different threads you MUST make everything thread safe
Vlad
What do you mean by "access from wrong thread"? If you REALLY NEED to access every single peace of code from different threads you MUST make everything thread safe
If you read when it's guaranteed that no other thread is writing to that data you can omit locks and still be thread safe
Pavel
If you read when it's guaranteed that no other thread is writing to that data you can omit locks and still be thread safe
If you change (or initialize) an object without lock or atomic instructions in one thread there is no guarantee other threads could see that change
Pavel
What do you mean by "access from wrong thread"? If you REALLY NEED to access every single peace of code from different threads you MUST make everything thread safe
I mean for example I have a set of classes that handle rendering and they must be accessed from one thread only. But there are so many moving things in the app, so it becomes difficult to guarantee that no code from other threads don't touching anything related to these classes. I've implemented a helper class that acts like a mutex and assert whether something access anything from this from different threads, but it looks like a crutch. I wonder what other people do to help it in their apps.
Pavel
If you read when it's guaranteed that no other thread is writing to that data you can omit locks and still be thread safe
Yes, basically that's the goal, to ensure that I can't get a situation where more than one thread is trying to do something with some data (because if they do then it's an error in the logic)
Vlad
So you can divide an array between threads to do work
Pavel
If your threads do work after the change it's not an issue
It is an issue. CPU cores have its own L1/2 caches and these caches have to be sync in order to see each other changes. Such a sync can not be garanteed without atomic instructions
Pavel
Try minimising shared resources. There is no point in having lots of threads when they spend time on waiting resources. Access those that need to be shared through defined set of accessor methods or create a class with the sole purpose of controlling exclusive access to them.
About the class "with sole purpose of controlling exclusive access", what do you mean? A class that is acting as an interface to all the shared resources? Or there's some pattern that you have in mind?
Vlad
There's no change to speak of
Pavel
It doesn't matter if the data is read only when your threads do work.
What do you mean by read-only. When was it initialized?
Pavel
If it's a global const then yes it's not a problem
Vlad
What do you mean by read-only. When was it initialized?
Before the thread/coroutine was spawned
Pavel
It doesn't matter if the data is read only when your threads do work.
But how to guarantee that it's only read for example? How to ensure that other programmers who work with the app will not introduce changes that are not safe anymore?
Pavel
Before the thread/coroutine was spawned
Yes, it's not a problem in that case
Pavel
I'm thinking actually about a pattern when you have resources and jobs. And you init jobs with classes that encapsulate access to the resources (either const to read or non-const). Then you can ensure that jobs are being parallelized with respect to that (either by hand, or by computing a graph at runtime that don't allow read and write to the same resource at the same time). But that may be overkill, and not sure that it's even realizable. And yes, then there should be just no other possible way to access the resources rather than through these classes that encapsulate the access.
Pavel
Or do you want to garantee there is no waiters on mutex?
Pavel
Or do you want to garantee there is no waiters on mutex?
In general I want to avoid mutexes when possible. Like when I write code I ensure there's just no way that some data can be read and written at the same time (or even can be accessed only in one place). But I want to ensure that this won't change accidentally in future (since it's just happen to work this way today, when code will grow and be moved around there's a big chance something will break and nobody will notice).
Pavel
Look at oneTBB library
Thanks, will investigate it, looks interesting
Pavel
Just because you can't prove what your code does in general
Pavel
But "in general" is a key point here. With number of constraints it should be doable
Diego
I get that you want to avoid mutexes but if you expect the code to grow (And more importantly, you expect not to be the only one working on it) things such as those are a good way to avoid throwing hands with the compiler
Pavel
I get that you want to avoid mutexes but if you expect the code to grow (And more importantly, you expect not to be the only one working on it) things such as those are a good way to avoid throwing hands with the compiler
Yes, I agree, but mutexes don't seem to be a solution, there is a lot of data that moves around, and either I lock too much or will have too many locks.
Aakash
int main () { if( sizeof(int)> -1) cout<<"yes": else cout<<"no"; return 0; }
#include <iostream> using namespace std; int main () { cout.setf(ios::showbase); cout <<"\nSizeof(int): " <<hex<<sizeof(int); cout <<"\nValueOf(-1): " <<hex<<(int)-1; return 0; }
Pavel
You can watch this talk about lock free programming. https://youtu.be/ZQFzMfHIxng
Yes, that's a great talk, but atomics just have the same issue as too many mutexes. I'm trying to layer my data in a way that it can be processed most efficiently (both for cache and branch prediction), if every variable in my app will be atomic then that will make all that effort meaningless. I want to avoid the case when both atomics and mutex are needed (it won't be possible everywhere of course, but at least in most of the data)
Pavel
In any way, I already got a lot of helpful input. I'll investigate the links and think about what you've said Thank you all ❤
Pavel
*transactional mem!
At first started googling "transactional meme" :) Thanks, will investigate as well
Elisson
I've read the rules and I agree with them
Diego
Click the button my man
Sandro
I started using Qt Creator, is there an option to save the previous session code line in the Editor like VS Code does? Every time I need to remember where I stop last time...
Bumpy
anyone know what is the problem?
Bumpy
i want to split a given string
Bumpy
void stringSplit(char *string) { char *runner = string + 0; int counter = 0; while (*runner) { int current = 0; while (*runner != ' ') { counter ++; runner++; current ++; } char *word = (char*)calloc(counter + 1,sizeof(char)); char *start = string + counter - current , *end = string + counter; while (*start != *end) { *word = *start; word ++; start++; } word[counter + 1] = 0; printf("%s",word); free(word); runner ++; } }
Bumpy
i know that its terrible
Pavel
I think it can be done simpler if you just remember word start and calculate word end. And use them consistently, now it does some strange math that is hard to follow
MRT
how i can hold multidimensional array like int[10][20][30] to heap ? can anyone give me a example
Pavel
how i can hold multidimensional array like int[10][20][30] to heap ? can anyone give me a example
I usually allocate an array of size 10*20*30 (M=10, N=20, K=30) and address it like arr[x*N*M+y*M+k] (order depends on access patterns)
Bumpy
char stringSplit(char *string) { char *start = string; char *end = string + lengthOf(string); char *runner = string; int len = 1; while(*runner) { if (*runner == ' ') len ++ ;runner ++; } char **words = (char)(calloc(len,sizeof(char*))); int counter = 0; int i = 0; while (*start) { int current = 0; while (*start != ' ') { counter ++; current ++; start ++; } if (current != 0) { start = string + (counter - current); end = string + counter; words[i ++] = (char*)(calloc(current,sizeof(char))); while (*start != *end) { } } } printf("%d",len); }
Bumpy
i want to split the string into an array of char pointers
Bumpy
in the beginning im trying to count the number of words in vairable (len)
Bumpy
after that im allocate dynamically number of "strings" as number as len
Bumpy
current is allways count and if i want to the start sub string i doing counter - current
Bumpy
know how i can copy the chars in the inner while loop?
Pavel
char stringSplit(char *string) { char *start = string; char *end = string + lengthOf(string); char *runner = string; int len = 1; while(*runner) { if (*runner == ' ') len ++ ;runner ++; } char **words = (char)(calloc(len,sizeof(char*))); int counter = 0; int i = 0; while (*start) { int current = 0; while (*start != ' ') { counter ++; current ++; start ++; } if (current != 0) { start = string + (counter - current); end = string + counter; words[i ++] = (char*)(calloc(current,sizeof(char))); while (*start != *end) { } } } printf("%d",len); }
Ok, you have start pointer. At the beginning it points to the word start. You don't need to increment it when you searching for the end of the word right? You have end pointer, you should do end = start at the beginning of the loop body and then increment it instead of start. Then you will have start pointing at the beginning of the word, end pointing at the end of the word and current that contain the length (you can just do end-start+1 to calculate it actually, no need to increment it in the loop). current is a bad name for a variable though ("current what?").
AHMED
#include<iostream> using namespace std; int main() { constexpr auto mylambda = [](int x, int y) { return x + y; }; static_assert(mylambda(10, 20) == 30, "The lambda condition is not true."); }
AHMED
What is the problem in the above code?
Vlad
Could you give an error that compiler gives or smth
Vlad
How are we supposed to know?
AHMED
<stdin>:9:16: error: static_assert expression is not an integral constant expression static_assert(mylambda(10, 20) == 30, "The lambda condition is not true."); ^~~~~~~~~~~~~~~~~~~~~~ <stdin>:9:16: note: non-constexpr function 'operator()' cannot be used in a constant expression <stdin>:7:28: note: declared here constexpr auto mylambda = [](int x, int y) { return x + y; }; ^ 1 error generated.
Vlad
Do you have c++17 standard enabled?
AHMED
I am using cxxdroid on mobile
Vlad
I am using cxxdroid on mobile
It doesn't support it
Bumpy
Ok, you have start pointer. At the beginning it points to the word start. You don't need to increment it when you searching for the end of the word right? You have end pointer, you should do end = start at the beginning of the loop body and then increment it instead of start. Then you will have start pointing at the beginning of the word, end pointing at the end of the word and current that contain the length (you can just do end-start+1 to calculate it actually, no need to increment it in the loop). current is a bad name for a variable though ("current what?").
char stringSplit(char *string) { char *start = string; char *runner = string; int len = 1; while(*runner) { if (*runner == ' ') len ++ ;runner ++; } char **words = (char)(calloc(len,sizeof(char*))); runner = start; int index = 0; while (*runner) { int end = 0; while (*runner != ' ') { end ++; runner ++; } if (end != 0) { words[index] = (char*)(calloc(end + 1,sizeof(char))); for (int i = 0; i <= end && start < runner; i++) { words[index][i] = *((start)++); } index ++ ; start = runner + 1; } else runner ++; } for (int i = 0; i < len; i ++) { printf("%s\n",words[i]); } }
Bumpy
I tried to simplify the code more.
Bumpy
But the first word doesn't print.
AHMED
It doesn't support it
I tried nested namespaces which is a c++ 17 feature in cxxdroid and it worked!
Bumpy
"grabish value" is a test using split
Pavel
I tried nested namespaces which is a c++ 17 feature in cxxdroid and it worked!
So, obviously it doesn't support ALL c++17 features