为什么在创建时不能通过引用传递对象std::thread
?
例如,以下snippit给出了编译错误:
#include <iostream>
#include <thread>
using namespace std;
static void SimpleThread(int& a) // compile error
//static void SimpleThread(int a) // OK
{
cout << __PRETTY_FUNCTION__ << ":" << a << endl;
}
int main()
{
int a = 6;
auto thread1 = std::thread(SimpleThread, a);
thread1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from /usr/include/c++/4.8/thread:39:0,
from ./std_thread_refs.cpp:5:
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(int))(int&)>’:
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(int&); _Args = {int&}]’ …
Run Code Online (Sandbox Code Playgroud) 在手册页中,即使您将信号量初始化为值1,也会显示:
sem_init(&mySem, 0, 1);
Run Code Online (Sandbox Code Playgroud)
多次调用时,它仍然可以增加到大于1的值
sem_post(&mySem);
Run Code Online (Sandbox Code Playgroud)
但在这个代码示例中,注释似乎有不同的看法:
sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */
Run Code Online (Sandbox Code Playgroud)
是否可以在C中初始化严格的二进制信号量?
注意:在这种情况下执行此操作而不是使用互斥锁的原因是sem_post和sem_wait可能由不同的线程调用.
我有以下测试应用程序:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main(void){
char buf[512];
buf[0]= 0x1;
buf[1]= 0x2;
char *temp1 = &buf;
char *temp2 = buf;
char *temp3 = &buf[0];
printf("temp1:%p, temp2:%p, temp3:%p\n",temp1,temp2,temp3);
printf("0 = %d, %d, %d\n",temp1[0],temp2[0],temp3[0]);
printf("1 = %d, %d, %d\n",temp1[1],temp2[1],temp3[1]);
return;
}
Run Code Online (Sandbox Code Playgroud)
它汇编了一个警告:
gcc ./testptr.c -o testptr
./testptr.c: In function ‘main’:
./testptr.c:9: warning: initialization from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,所有三个指针的行为都相同.
./testptr
temp1:0x7fff3a85f220, temp2:0x7fff3a85f220, temp3:0x7fff3a85f220
0 = 1, 1, 1
1 = 2, 2, 2
Run Code Online (Sandbox Code Playgroud)
我知道buf == &buf[0]
,但为什么&buf == …
我有一些 .c 和 .h 文件,它们是由基于一个 XML 文件和可选的另一个 XML 文件的脚本生成的。
从我读过的内容来看,我应该能够使用通配符,例如:
%Generated.c %Generated.h : %Compulsory.xml $(wildcard %Optional.xml)
generation-script $< $*
Run Code Online (Sandbox Code Playgroud)
当我更改 XXXCompulsory.xml 而不是当我更改 XXXOptional.xml 时,文件 XXXGenerated.c/h 正在重新生成。有谁知道为什么?
我可以解决重复规则的问题,如下所示:
%Generated.c %Generated.h : %Compulsory.xml
generation-script $< $*
%Generated.c %Generated.h : %Compulsory.xml %Optional.xml
generation-script $< $*
Run Code Online (Sandbox Code Playgroud)
但我会认为有更好的方法来指定可选依赖项?
谢谢!
注意:之前已经问过同样的问题(例如,如何管理 C 头文件依赖项?)但在这种情况下,我不能让编译器生成 .d 文件。
编辑: Optional.xml 的位置很好,因为如果我只指定规则,它就可以工作:
%Generated.c %Generated.h : %Compulsory.xml %Optional.xml
generation-script $< $*
Run Code Online (Sandbox Code Playgroud)
并尝试仅针对 Optional.xml 存在的目标进行编译。
但我注意到这不适用于括号:
%Generated.c %Generated.h : %Compulsory.xml $(%Optional.xml)
generation-script $< $*
Run Code Online (Sandbox Code Playgroud) 正如我们所知,分散列表收集了物理上分散在内存中的内存,但实际上是连续的.当与DMA通信时,它为DMA提供了一个抽象的内存视图,因为这是物理上连续的内存.
怎么scatterlist
处理这个?它是一种保持在内的链表scatterlist
吗?
例如,如果要使用DMA传输4000字节的数据,因为数据在物理上是连续的,分散列表如何使其连续?scatterlist实现将使用分配4000字节的数据Kmalloc
来确保它获得物理上连续的内存?还是会创建一个链表?
注意:这里我的查询不是关于SG表,而是关于单个分散列表
让我们说我们想要保存4000字节物理上不连续的内存的数据
struct scatterlist sg,
sg_init_one(&sg,data,4000)
Run Code Online (Sandbox Code Playgroud)
现在,这个sg如何解决这个问题?
我有一个指向结构的指针数组.我想将数组中的每个元素初始化为NULL.我将使用此数组来动态添加和删除条目,因此我希望能够针对NULL进行测试,以查看该索引是否为空.
我试着这样做:
struct mystructure *cxs[MAXNOCXS];
int cx_init(void)
{
int i = 0;
for (i=0; i<MAXNOCXS; i++)
cxs[i] == NULL;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我的编译器告诉我:
error: statement with no effect
Run Code Online (Sandbox Code Playgroud)
指的是这条线 cxs[i] = NULL;
指针数组是否在C中自动初始化为NULL?如果不是我怎么能这样做?