[M132][Part_1] chromium codelab
Prerequisite: Getting the Code
略
Part 1: Using command-line arguments
main
函数开头需要加上下面的代码设置下日志的输出方式
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_ALL; // 默认就是控制台
settings.log_file_path = FILE_PATH_LITERAL("hello_world.log");
logging::InitLogging(settings);
Part 2: Callbacks and Bind
是个move-only
对象
// The type of a callback that:
// - Can run only once.
// - Is move-only and non-copyable.
// - Takes no arguments and does not return anything.
// base::OnceClosure is an alias of this type.
base::OnceCallback<void()>
执行的时候要加std::move
void MyFunction1(base::OnceCallback<int(std::string, double)> my_callback) {// OnceCallbackint result1 = std::move(my_callback).Run("my string 1", 1.0);// After running a OnceCallback, it's consumed and nulled out.DCHECK(!my_callback);...
}
Part 3: Threads and task runners
Threading and Tasks in Chrome
There are a number of ways to post tasks to a thread pool or task runner.
PostTask()
PostDelayedTask()
if you want to add a delay.PostTaskAndReply()
lets you post a task which will post a task back to your current thread when its done.PostTaskAndReplyWithResult()
to automatically pass the return value of the first call as argument to the second call.
参考链接
- C++ in Chromium 101 - Codelab
- GN Language and Operation