小编Rob*_*013的帖子

C++ 11使用成员函数编译错误的线程初始化

我刚刚开始使用C++ 11线程,我一直在努力(可能是愚蠢的)错误.这是我的示例程序:

#include <iostream>
#include <thread>
#include <future>
using namespace std;

class A {
public:
  A() {
    cout << "A constructor\n";
  }

  void foo() {
    cout << "I'm foo() and I greet you.\n";
  }

  static void foo2() {
    cout << "I'm foo2() and I am static!\n";
  }

  void operator()() {
    cout << "I'm the operator(). Hi there!\n";
  }
};

void hello1() {
  cout << "Hello from outside class A\n";
}

int main() {
  A obj;
  thread t1(hello1); //  it works …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading class member c++11

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

通过pthreads获取C++ 11中的线程核心关联

我正在尝试设置核心亲和力(线程#1进入第一个核心,线程#2进入第二个核心,......),同时在C++ 11中使用std :: thread.

我已经在各种主题和互联网上搜索过,似乎C++ 11 API没有提供如此低级别的功能.

另一方面,pthreads带有pthread_setaffinity_np,如果我可以得到我的std :: thread的"pthread_t"值(我不知道这是人类合理还是至少是合法的要求),这将非常有用.

我最终想要的一个示例程序是:

#include <thread>
#include <pthread.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define CORE_NO 8

using namespace std;

void run(int id) {
    cout << "Hi! I'm thread " << id << endl;
    // thread function goes here
}

int main() {
    cpu_set_t cpu_set;

    CPU_ZERO(&cpu_set);
    for(int i=0; i<CORE_NO; i++)
        CPU_SET(i, &cpu_set);

    thread t1(run, 1);

    // obtaining pthread_t from t1

    /*
    pthread_t this_tid = foo(t1);
    pthread_setaffinity_np(this_tid, sizeof(cpu_set_t), &cpu_set);
    */

    t1.join();

    return …
Run Code Online (Sandbox Code Playgroud)

c++ multicore pthreads affinity c++11

8
推荐指数
2
解决办法
7764
查看次数

C++ 11替代pthread_cond_timedwait

我需要让一个线程等到两个

  • 超时已过期,或
  • 变量由另一个线程更改

经过一些研究后我发现pthreads得到了 pthread_cond_timedwait,如果我使用pthreads,这在这种情况下会很有用.

我正在使用C++ 11线程.没有完全传递给pthreads,有没有合适的替代方案?

c++ multithreading timeout pthreads c++11

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

Python LinkedList中的内存分配错误

关于Python简单链表及其内存消耗,我有一些问题.

这是代码:

import sys

class Record:
    def __init__(self,elem):
        self.elem=elem
        self.next=None

    def size(self):
        print 'elem.size = ', sys.getsizeof(self.elem)
        print 'next.size = ', sys.getsizeof(self.next)


class LinkedList:
    def __init__(self):
        self.first=None
        self.last=None

    def addAsLast(self,elem):
        rec=Record(elem)
        if self.first==None:
            self.first=self.last=rec
        else:
            self.last.next=rec
            self.last=rec

if __name__=="__main__":
    l=LinkedList()
    r = Record(1)
    r.size()

    maxx = 10000000
    r = range(1, maxx)
    print 'size of r: ', sys.getsizeof(r)
    print 'size of r[n-1]: ', sys.getsizeof(r[maxx-2])

    for i in r:
        if(i% (maxx/10) == 0): print '.'
        l.addAsLast(i)
    print "The End"
Run Code Online (Sandbox Code Playgroud)

我的问题是:运行此脚本消耗1.7 GB的RAM …

python memory memory-management linked-list python-2.7

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

使用动态库交叉编译C代码时出错

我有两个文件:

lib.c

#include<stdio.h>

void hi() {
  printf("Hi i'm a library function in lib.so\n");
} 
Run Code Online (Sandbox Code Playgroud)

和main.c

#include<stdio.h>
#include<dlfcn.h>
/* based on Jeff Scudder's code */
int main() {
  void *SharedObjectFile;
  void (*hi)();

  // Load the shared libary;
  SharedObjectFile = dlopen("./lib.so", RTLD_LAZY);

  // Obtain the address of a function in the shared library.
  ciao = dlsym(SharedObjectFile, "hi");

  // Use the dynamically loaded function.
  (*hi)();

  dlclose(SharedObjectFile);
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下命令构建可执行文件:

export LD_LIBRARY_PATH =pwd

gcc -c -fpic lib.c

gcc -shared -lc -o lib.so lib.o

gcc …

c android arm cross-compiling codesourcery

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