inkfil
What exactly your task that requires C++?
Just curious not anything specific in mind
Anonymous
How to globally declare variable in C
Alex
use extern
Vlad
How to globally declare variable in C
in .h extern int smth; in .c int smth = 5;
Anonymous
Thank you
Anonymous
What is the use of static
Alex
local variable, common variable for all function calls
Vlad
local variable, common variable for all function calls
That is if it's declared within the function's body
Vlad
If not then static means visible for this .c file only
Anonymous
Okay
Anonymous
Extern is only way to globally initialize a variable
Alex
extern is not about initializing, its about declaration
Anonymous
Okay
V01D
Who wrote a music visualizer before and can help me out?
J
what kind of visualizer, what kind of help do you need?
Nils
Kind of
Nils
To declare a variable that was declared in another linked file already
Nils
So you can access that variable
Nils
Yeah
Vlad
In .c file
Nils
In another object file
Vlad
redefenition will fail on linking stage
Alex
file3.c = #include file2.h
Nils
file3.c = #include "file2.h"
Nils
No need to redeclare then
V01D
what kind of visualizer, what kind of help do you need?
An audio visualzer, meaning I give it a wav file and visualize it while playing it in real time. I need help with the whole calculating the FFT part. More specifically, I cannot understand how to calculate the FFT from a .wav file. I need the FFT so that I can calculate the max magnitude for one audio sample. Then I will plot the image and repeat
Vlad
Redefenition in .C file will fail on linking stage. What didncha understand? :P
Vlad
In .c file it's definition
Vlad
And declaration is .h file
Nils
Just not redclare in file3.c
Vlad
You define it once in your globals.c and all it a day.
Vlad
No
Nils
No just leave that line away
Nils
No, leave away "int smth;" from file3.c
Vlad
it's like that: file1.h: extern int global; file1.c: int global;
Vlad
And other files have nothing
Vlad
They include file1.h and use global variable
V01D
I have read so many different pieces of code, and until now I couldn't wrap my head around them. Seems to be this whole fft audio wave math thing I don't get..
V01D
It's been 5 days with no progress
V01D
I quit for now, lol. I just don't get it, and that is like the only thing I don't get... oh well
V01D
Wait what am I saying. Imma continue
MᏫᎻᎯᎷᎷᎬᎠ
what is the difference between usual threads, async functions and coroutines
数学の恋人
what is the difference between usual threads, async functions and coroutines
I'll try to say in simple words, Threads - multiple jobs running parallel, multiple threads Coroutines - multiple jobs, but running sequentially, one by one (one is executed at a time) async - similar to coroutine, waits for one job to finish then programmer decide what to do next, do another job, contine same job, etc.
MᏫᎻᎯᎷᎷᎬᎠ
I mean by "controlled by the programmer" is you won't be able to call resume() stop() / suspend()
olli
what is the difference between usual threads, async functions and coroutines
async return before they complete. e.g. you can have a function CopyFileAsync(From, To) CopyFileAsync(From, To); // call to the function returns but the copy might // not be completed thread generelly independent execution of instructions coroutines only syntactical sugar, let's you write asynchronous code as if it were synchronous co_await CopyFileAsync(From, To); // Copied completed, but in the meantime, some // other function was able to advance
MᏫᎻᎯᎷᎷᎬᎠ
and how Async can provide such thing if it doesn't use threads in its internal implementation
MᏫᎻᎯᎷᎷᎬᎠ
Operating System Api
How does it work? ofc it's not just magical haha
Anonymous
How does it work? ofc it's not just magical haha
I don't know I'm not a Windows Developer..
MᏫᎻᎯᎷᎷᎬᎠ
Anonymous
Basically it runs on a detached thread
olli
so coroutines provide more control over the code
no, you can do the same thing without coroutines, using coroutines feels more natural and is more readable.
Anonymous
Just watch some talks by Gor Nishanov in CppCon
Anonymous
It's the person that drives coroutines into C++ standard
MᏫᎻᎯᎷᎷᎬᎠ
Basically it runs on a detached thread
what?!! I heard that Async are more lightweight than threads
Anonymous
what?!! I heard that Async are more lightweight than threads
> runs Not a thread, just runs on a thread
Anonymous
I think you need to read something about Concurrency...
MᏫᎻᎯᎷᎷᎬᎠ
I know detach()
Anonymous
There's a book called C++ Concurrency in Action
MᏫᎻᎯᎷᎷᎬᎠ
lol But what I was talking about threads use more system resources
Anonymous
So?
Anonymous
Do you know what a thread pool is?
MᏫᎻᎯᎷᎷᎬᎠ
Yes??
olli
and how Async can provide such thing if it doesn't use threads in its internal implementation
let's assume you have a server, you listen for multiple connections to come in and for perf reasons you want to execute them concurrently. Most operating systems offer you a function to register that socket to an internal data structure, whenever one of those sockets is ready you get a "notification" which socket it is and you can resume handling the connection. One abstraction level higher the connections seem to be asynchronous, potentially executing out of order.
MᏫᎻᎯᎷᎷᎬᎠ
what does thread pool has to do with this?
MᏫᎻᎯᎷᎷᎬᎠ
this is from Rust asynchronounce book after all, threads were designed to do just this: run multiple different tasks at once. However, they also come with some limitations. There's a lot of overhead involved in the process of switching between different threads and sharing data between threads. Even a thread which just sits and does nothing uses up valuable system resources. These are the costs that asynchronous code is designed to eliminate. We can rewrite the function above using Rust's async/.await notation, which will allow us to run multiple tasks at once without creating multiple threads:
olli
e.g. you write readFromNetwork(socket);, you queues this request and either a provided callback or flag indicates your request has finished.
olli
no instead of passing callbacks you can use futures and chain them or use the syntactic sugar to just use await
olli
another neat use case for coroutines was to change threads, imagine you're writing a GUI application. Most frameworks use one main/render thread and all changes to UI should be made through that thread. What people are doing is something like this void RunOnUiThread(Cb); void UpdateUI() { auto Result = DoSomeComputations(); RunOnUiThread([&](){ UpdateUiWithResult(Result); }); } What coroutines allow you to do void UpdateUI() { auto Result = DoSomeComputations(); co_await SwitchToUiThread(); UpdateUiWithResult(Result); }
olli
So there is no an advantage point other than this?
no, but that's a pretty big reason. I like to write auto instead of std::vector<int>::iterator :)