小编use*_*779的帖子

错误:使用已删除的函数'std :: thread :: thread(const std :: thread&)'

下面的代码编译并按预期工作.结构(类)A来源于std::thread并扩展了int更多.该main代码创建了一些线程,并随后等待他们完成.

问题是虽然代码在struct中没有析构函数的情况下进行编译A,但是当析构函数被取消注释时(~A(){})我得到:

错误:使用已删除的函数'std :: thread :: thread(const std :: thread&)'

而且我不知道为什么.

此外,我不明白为什么代码可以同时使用push_backemplace_back同时根据我的理解它不应该使用push_back.

#include <iostream>
#include <thread>
#include <vector>

struct A : std::thread {
    int i;
    A(void f(const char*),const char* s,int i_) : std::thread{f,s},i{i_}{
        std::cout<<"A created"<<std::endl;
    }
    //~A(){} // uncomment to see error
};

void dosomething(const char* s){
    std::cout<<s<<std::endl;
}

int main(){
    std::vector<A> aa;
    aa.emplace_back(&dosomething,"hi people",3434);
    aa.push_back(A(&dosomething,"hi again people",777));
    aa.emplace_back(&dosomething,"hi again …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading stdthread

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

原子比较,如果小于则有条件地减去

我管理一些并发线程使用的内存,我有一个变量

无符号整数 freeBytes

当我从任务中请求一些记忆时

无符号整数字节需要

我必须检查是否

bytesNeeded<=freeBytes

如果是,则保留 freeBytes 的旧值并从 freeBytes bytesNeeded 中自动减去。

原子库或 x86 是否提供了这种可能性?

c++ x86 atomic stdatomic

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

在数据区域执行代码需要做什么,(段保护)

我在linux平台上工作,我使用g ++和上面的程序将函数从代码区域复制到数据区域.如何更改数据段的保护以允许我执行复制的功能?

代码如下:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define Return asm volatile("pop %rbp; retq; retq; retq; retq; retq;")
int64_t funcEnd=0xc35dc3c3c3c3c35d;
constexpr int maxCode=0x800;
int8_t code[maxCode];

void testCode(void){
    int a=8,b=7;
    a+=b*a;
    Return;
}

typedef void (*action)(void);

int main(int argc, char **argv)
{
    action a=&testCode;
    testCode();

    int8_t *p0=(int8_t*)a,*p=p0,*p1=p0+maxCode;
    for(;p!=p1;p++)
        if ( (*(int64_t*)p)==funcEnd ) break;

    if(p!=p1){
            p+=sizeof(int64_t);
            printf("found\n");
        memcpy(&code,(void*)a,p-(int8_t*)a);
        ((action)&code)();
    }

  printf("returning 0\n");
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c c++ assembly linux-kernel

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

如何在一个语句中定义对数组的引用

我定义了一个中间typedef,以定义对固定大小数组的引用.

typedef int CI[0x10];
CI& arr=*(CI*)mypointer;
Run Code Online (Sandbox Code Playgroud)

写它就像以后允许我使用它 countof(arr)

我试着在一个声明中写下它,而下面是我失败的尝试.我知道这是错的,因为"&"应该在"int"和"[0x10]"中,而不是在arr1上,但可以在一个语句中写入它;

int (arr1&)[classInfoN]=*(CI*)mypointer;
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×4

assembly ×1

atomic ×1

c ×1

linux-kernel ×1

multithreading ×1

stdatomic ×1

stdthread ×1

x86 ×1