小编SAC*_*YAL的帖子

对于一个程序,Boost :: mutex比没有mutex花费的时间更少

我已经执行了以下程序,我创建了100个并发执行的线程.请注意,这是一个示例程序.我知道下面的程序不需要多个线程,但我的目的是测试互斥锁.

class ThreadPool{

    public:
    ThreadPool(int num = 10);
    ~ThreadPool();
    void AssignPool();
    void doSometask();
    void inc();
    private:
    boost::asio::io_service ioService;
    boost::thread_group threadpool;
    boost::asio::io_service::work * work;
   volatile int p_size;
    int pool_sz;
    boost::mutex io_mutex;// with boost lock

};

void ThreadPool::AssignPool()
{
        std::cout<<std::endl<<"pool_sz="<<pool_sz<<std::endl;
        for(int i=0;i<pool_sz;i++)
        {
                ioService.post(boost::bind(&ThreadPool::doSometask, this));
        }
}

void ThreadPool::inc()
{
        p_size++;
}

void ThreadPool::doSometask()
{

//      boost::mutex::scoped_lock lock(io_mutex);
        for(int i=0;i<10000;i++){
                inc();
        }


}

ThreadPool::ThreadPool(int num):p_size(0)
{
        pool_sz = num;
        work = new  boost::asio::io_service::work(ioService);
        for(int i =0;i<num;i++)
        {
                threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService  ))    ;
        } …
Run Code Online (Sandbox Code Playgroud)

c++ linux boost mutex

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

为什么在获取进程的子级时使用同级列表获取task_struct

内核task_struct如下所示。我对两个成员(子级和同级成员)更感兴趣,因此我从此内核结构中删除了其他元素。

  struct task_struct{
        // some data elements .


          struct list_head children;      
              /* list of my children */

          struct list_head sibling;
              /* linkage in my parent's children list */
        //some data members 
        };
Run Code Online (Sandbox Code Playgroud)

“子项”是进程子项的task_struct的双向循环链接列表。如果要从当前进程访问子项,则必须使用宏“ list_for_each”遍历“子项”列表,如下所示:

struct task_struct *task; 
struct list_head *list;
list_for_each(list, &current->children) { 
task = list_entry(list, struct task_struct, sibling); /* task now points to one of current’s children */ 
}
Run Code Online (Sandbox Code Playgroud)

list_for_each最终将使用下一个子项初始化“ list”。现在,由于我们要遍历子项列表,因此理想情况下,我们应该从“ list”指针中减去“子项”列表的偏移量,以获取当前进程的tast_struct地址。是什么原因导致我们在此处传递“兄弟”,而最终导致具有不同偏移量的另一个列表?

请注意:这是有效的代码,我要了解的是为什么当应使用children指针计算正确的偏移量并因此计算child的task_struct地址时为什么使用同级。

提前致谢 。

c linux linux-kernel

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

PID_MAX在Linux中所依赖的因素是什么?

我检查了两个linux系统,两者都是64位,但两者的PID_MAX不同.

在一个系统上(SYSTEM1)

cat /proc/sys/kernel/pid_max
32768
Run Code Online (Sandbox Code Playgroud)

SYSTEM1配置:

dmesg | grep -i smp.*允许

getconf PAGESIZE
4096

cat /proc/cpuinfo | awk '/^processor/{print $3}' | tail -1
11
cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 45
model name  : Intel(R) Xeon(R) CPU E5-2430 0 @ 2.20GHz
stepping    : 7
microcode   : 1803
cpu MHz     : 2201.000
cache size  : 15360 KB
physical id : 0
siblings    : 6
core id     : 0
cpu cores   : 6
apicid …
Run Code Online (Sandbox Code Playgroud)

linux operating-system linux-kernel

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

std :: begin()如何为内置类型工作?

假设我在下面的代码行使用std :: begin.

int myint[] ={1,2,3,4,5,6,7,8,9};
std::find(begin(myint),end(myint),9);
Run Code Online (Sandbox Code Playgroud)

现在std :: begin声明如下.

template< class C > 
auto begin( C& c ) -> decltype(c.begin());
Run Code Online (Sandbox Code Playgroud)

我无法理解,它是如何运作的?AFAIK,返回类型std :: begin是decltype(c.begin()),当C作为整数数组传递时,不会有任何带整数的begin函数.所以c.begin()应该无效?

c++ c++11

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

在数组大小的情况下,gcc 4.8.2和gcc 4.9.0的行为差异

在Gcc 4.8.2版本之前,以下代码无法编译,因为数组大小不是编译时间常量.

#include<iostream>
using namespace std;

int f(){return 10;}
int main()
{
    int i=10;  
    int arr[f()]={}; //error 
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在4.9及更高版本上运行类似的代码时,相同的代码已成功编译.

它是允许这样的代码的编译器还是它现在是标准的一部分?

注意:上面的代码无法编译直到clang 3.7.1

c++ arrays g++

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

标签 统计

c++ ×3

linux ×3

linux-kernel ×2

arrays ×1

boost ×1

c ×1

c++11 ×1

g++ ×1

mutex ×1

operating-system ×1