小编som*_*ous的帖子

如何在乳胶中对齐两个tikzpicture图

我正在尝试使用https://www.latex-tutorial.com/tutorials/figures/中显示的 subfigure 方法来制作并排图,但我似乎无法调整大小并使它们并排...我究竟做错了什么?下面是我正在使用的代码

\begin{figure}
        \centering
        \begin{subfigure}[b!]{0.3\textwidth}
            \begin{tikzpicture}
                \begin{axis}[
                    axis y line = middle,
                    axis x line = middle,
                    xlabel = $x$,
                    ylabel = {$f(x) = x^3$},
                    grid=major,
                ]
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=red,
                ]
                {x^3};
                \addlegendentry{$x^3$}
                %
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=blue,
                    ]
                    {x^3 + 3};
                \addlegendentry{$x^3 + 3$}
                 %
                \addplot [
                    domain=-3:3, 
                    samples=100, 
                    color=green,
                    ]
                    {x^3 - 3};
                \addlegendentry{$x^3 - 3$}
                \end{axis}
            \end{tikzpicture}
        \end{subfigure}
        %\hfill
        \begin{subfigure}[b]{0.3\textwidth}
            \begin{tikzpicture}
                \begin{axis}[
                    axis y line = middle,
                    axis x line = …
Run Code Online (Sandbox Code Playgroud)

latex tikz

7
推荐指数
1
解决办法
3万
查看次数

C 标准保证 printf("%.*s", 0, NULL) 是安全的吗?

我有一个重复的打印,打印了一堆非空终止的,char*如下所示:

    int len1, len2, len3, len4;
    char *str1, *str2, *str3, *str4;
    get_vals(message, val1, &str1, &len1);
    get_vals(message, val2, &str2, &len2);
    get_vals(message, val3, &str3, &len3);
    get_vals(message, val4, &str4, &len4);
    printf("%.*s %.*s %.*s %.*s", len1, str1, len2, str2, len3, str3, len4, str4);
Run Code Online (Sandbox Code Playgroud)

其中将指针get_vals设置char *为内存中的值并将 设置len为文本的长度。传入的值有可能char *被设置为NULL,如果是这样,长度字段将被设置为0。看来这个打印已经发生了很多次并且没有出现段错误,我认为由于长度说明符是0这样的事实,所以可能没有取消引用。然而,这总是安全的吗?也许它与操作系统或 libc 版本相关?是否值得像这样进行安全检查:

    printf("%.*s %.*s %.*s %.*s",
           len1, str1 ? str1 : "",
           len2, str2 ? str2 : "",
           len3, str3 ? …
Run Code Online (Sandbox Code Playgroud)

c language-lawyer

6
推荐指数
1
解决办法
201
查看次数

syscall(SYS_gettid) 可以阻塞吗?

我的理解是,一些(如果不是全部)系统调用可能会被阻塞。这是阻塞呼叫吗?我想在一个被重复调用且对时间敏感的函数中获取唯一的线程ID。我本来打算这样做:

//header.h
#define _GNU_SOURCE
#include <sys/syscall.h>
#ifdef SYS_gettid
#define gettid() syscall(SYS_gettid)
#else
#define gettid() 0
#endif
Run Code Online (Sandbox Code Playgroud)
//file.c
#include "header.h"
...
void function()
{
        pid_t thread = gettid();
        //Logic based on thread
        ...
}
Run Code Online (Sandbox Code Playgroud)

这里的函数会被重复调用并且不能阻塞。也许我应该使用pthread_self()and pthread_equal(t1, t2)?谢谢。

编辑:我同样可以使用其中任何一个。我知道它们不提供相同的返回值,但我将对线程 id 执行的逻辑是将其与一组已知的线程 id 进行比较,以基本上找出谁在调用它。我保存了 gettid() 返回值和 pthread_self() 返回值。

c linux

1
推荐指数
1
解决办法
1432
查看次数

跨多个线程和 CPU 安全修改和读取布尔值的选项有哪些?

我有一些 C++ 代码,大致包含以下逻辑:

class wrapper_info {
public:
        bool isConnected();
        void connectedHandler();
        void disconnectedHandler();
protected:
        bool _connected;
}

void wrapper_info::connectedHandler() {
        _connected = true;
}

void wrapper_info::disconnectedHandler() {
        _connected = false;
}

bool wrapper_info::isConnected() {
        return _connected;
}

extern "C"
bool is_connected(void *obj) {
        wrapper_info *wrapper_obj = reinterpret_cast<wrapper_info*>(obj);
        return wrapper_obj->isConnected();
}

Run Code Online (Sandbox Code Playgroud)

由于大多数我无法控制的原因,不同的线程(在不同的 CPU 内核上运行)按以下方式调用这些函数。

线程 1、2、3is_connected(obj)

线程2connectedHandler()连接发起时。

disconnectedHandler()当连接断开时线程3 。

我认为,如果重复调用 和connectedHandler(),可能会出现问题disconnectedHandler(),两个线程写入的问题_connected以及写入的顺序会出现问题,从而导致错误的最终值。民意调查也可能存在问题_connected

我的问题是:

  1. 单独的线程轮询和修改 的值实际上可能会引起哪些潜在问题_connected? …

c++ multithreading multiprocessing

0
推荐指数
1
解决办法
292
查看次数

标签 统计

c ×2

c++ ×1

language-lawyer ×1

latex ×1

linux ×1

multiprocessing ×1

multithreading ×1

tikz ×1