例如,假设我有一个函数可以为您交换 32 位值中的字节:
uint32_t byte_swap(uint32_t in);
Run Code Online (Sandbox Code Playgroud)
嗯,将 32 位值压入堆栈并再次将其弹出似乎很愚蠢,特别是如果我们要多次调用此函数,所以让我们通过 ECX 传递它:
#if __FASTCALL_SUPPORTED_ /* Whatever this may be */
#define FASTCALL __attribute__((fastcall))
#else
#define FASTCALL
#endif
uint32_t FASTCALL byte_swap(uint32_t in);
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,将该函数编译到共享库中进行分发是否安全?如果用户使用不同的编译器来编译他们的程序并链接到此,该函数是否仍然可以正确调用?
Erlang ETS表可以在不同的进程之间共享吗?因此,如果我有两个进程在不同的Erlang运行系统上运行,我可以以某种方式链接它们,以便我在一个ETS表中做的所有更改将反映在另一个ETS表中吗?
我正在使用此处给出的 C++ 代码。但这里使用的共享语音识别运行它自己的命令,例如移动、最小化、删除。我需要在不调用 MS 语音识别程序的情况下创建它。
hr = cpEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
Run Code Online (Sandbox Code Playgroud)
上面的这一行创建共享实例。
我尝试使用CLSID_SpInprocRecognizer代替,但无法正确执行。我对此很陌生。有没有办法做到这一点?
在Objective-C中有Alloc/Init比喻.他们还添加了一个名为"new"的共享便捷方法,内部只是连续调用它们.如果我创建一个名为FooClass的NSObject的子类,FooClass会选择那些共享方法,包括'new'.
但是......实施的是怎么回事?
它不能简单地委托给基类,因为它只是实例化NSObject的实例,而不是派生类FooClass,但它仍然有效!那么有人会怎么写类似的东西?
换句话说,基类不应该是......
+ (id) somethingLikeNew{
return [[NSObject alloc] init];
}
Run Code Online (Sandbox Code Playgroud)
而是这......
+ (id) somethingLikeNew{
return [[<SomethingThatMapsToFooClassType> alloc] init];
}
Run Code Online (Sandbox Code Playgroud)
...其中'SomethingThatMapsToFooClassType'是从NSObject继承并且需要获取共享方法'somethingLikeNew'的派生类的类型.
基本上我是从NSObject添加一个类别,我有共享方法需要知道类型,但实现都是通用的,因此在NSObject上的类别而不是我的类文件中的所有地方(以相同的方式)你到处都没有'新'.它就在那里.)
任何人?Bueller?Bueller?
中号
我写了这个小应用程序,它的工作完美.但我是java新手并且假设必须有更好的方法来编写它,以便可以在两个函数中读取变量.在那儿?
package max.multiplebuttons.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class multibuttons extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView question = (TextView)findViewById(R.id.question);
TextView textView = (TextView)findViewById(R.id.textView);
Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 = (Button)findViewById(R.id.answer2);
answer1.setText("button1");
answer2.setText("button2");
question.setText("click a button");
textView.setText("Some Text");
answer1.setOnClickListener(this);
answer2.setOnClickListener(this);
}
public void onClick(View v){
TextView textView = (TextView)findViewById(R.id.textView);
Button answer1 = (Button)findViewById(R.id.answer1);
Button answer2 …Run Code Online (Sandbox Code Playgroud) 我想为主perl程序和其他包设置所有常见声明的专用包,而不是在每个头中重复这些声明.我肯定错了,但无法弄清楚背后的理由:
我们假设:
- 我已经在包my_common_declarations.pm中设置了我的公共数据.
- 我想在另一个包中使用这些数据, 例如my_perl_utils.pm.
#!/usr/bin/perl -w
package my_perl_utils;
use parent qw(Exporter);
our @EXPORT_OK = qw(f1 f2);
use my_common_declarations qw(debugme);
my %setup = &debugme;
my $DEBUGME = $setup{setup}{debugme};
# This generates this error : "Use of uninitialized value"
use constant true => $setup{setup}{'true'};
print "=" x25, "\nDEBUG true :\nimport = " . $setup{setup}{'true'} . "\nconstant = " , true , "\n", "=" x25, "\n";
sub f1{
# some rationals using the true or false …Run Code Online (Sandbox Code Playgroud) 我知道两种方式.什么是更好的?什么比两种方式更好?
+ (MyClass *)shared {
/*
static MyClass *sharedInstance = nil;
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}
}
return sharedInstance;
*/
/*
static dispatch_once_t pred;
static MyClass *sharedInstance = nil;
dispatch_once(&pred, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
*/
}
Run Code Online (Sandbox Code Playgroud) 这是我上一个问题( pthread mutex (un)locking over differentthreads )的后续问题。我对如何处理这里的问题和答案感到困惑,所以我重新尝试:o)
我试图通过进程和线程处理互斥体,并使用互斥体属性 PTHREAD_PROCESS_SHARED 来安排它。我添加了一个小示例(基于我上一篇文章中 Paolo 的示例),它演示了我的问题:
#include <stddef.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
pthread_mutex_t m;
sem_t s1, s2;
void print(const char *s, int err)
{
printf("%s %d %s\n", s, err, strerror(err));
}
void *start_t1(void *arg)
{
sem_wait(&s1); // <-t2
print("t1: unlock ", pthread_mutex_unlock(&m));
sem_post(&s2); //->t2
}
void *start_t2(void *arg)
{
sem_wait(&s2); // <-main
print("t2: lock ", pthread_mutex_lock(&m));
sem_post(&s1); // ->t1
sem_wait(&s2); // <-t1
sem_post(&s1); // ->main
}
void main(void)
{
pthread_mutexattr_t …Run Code Online (Sandbox Code Playgroud) 我尝试使用extern在c中的不同源文件之间共享一个全局变量.似乎每个程序都创建了本地不同的变量副本,因此,当程序更改其值时,第二个程序也无法看到更改.我可以修复此问题吗?该计划如下:
的Tools.h
#ifndef __TOOLS__
#define __TOOLS__
#include <errno.h>
#include <stdlib.h>
extern int i;
void init();
#endif
Run Code Online (Sandbox Code Playgroud)
tools.c
#include "tools.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int i;
void init(){
i=0;
}
Run Code Online (Sandbox Code Playgroud)
prog1.c的
#include "tools.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[]){
i=1;
printf("%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog2.c
#include "tools.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc,char *argv[]){
sleep(1);
printf("%d\n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog1打印1
prog2打印0(目标是打印1 - …
我有两个版本的相同算法.它最初是卷积,但我修改它以减少它以检查我的瓶颈在哪里(注意每个循环只有一次访问全局内存):
__global__
void convolve (unsigned char * Md, float * Kd, unsigned char * Rd, int width, int height, int kernel_size, int tile_width, int channels){
int row = blockIdx.y*tile_width + threadIdx.y;
int col = blockIdx.x*tile_width + threadIdx.x;
int sum = 0;
int pixel;
int local_pixel;
int working_pixel;
int row_offset = (kernel_size/2)*(width+kernel_size-1);
int col_offset = kernel_size/2;
for(int color=0; color<channels; color++){
pixel = color*width*height + row*width + col;
local_pixel = color*(width+kernel_size-1)*(height+kernel_size-1) + row*(width+kernel_size-1) + col + row_offset + col_offset;
if(row < height …Run Code Online (Sandbox Code Playgroud) 基本上我的概率是这样的:
我有一个头文件-里面foo.h有一个结构指针human *person = NULL。结构的定义human位于foo.h中包含的另一个标头中。我正在game.so使用foo.h和其他几个头文件和cpps 创建共享对象文件。
现在,我有两个不同的cpp文件- a.cpp和b.cpp它包括头foo.h. 我创建一个目标文件a.o分开和b.o独立。我同时链接了目标文件和game.so用于创建另一个共享目标文件的链接tennis.so,同时这样做了,“人”得到了多个定义。我知道多重定义错误是因为a.o包含的结构定义person,所以也是如此b.o。
我用#pragma once的foo.h了。a.o正在分别编译和b.o分别编译。所以我不认为的#pragma一次或IFDEF将在这里很有用,因为这两个a.cpp和b.cpp包括foo.h
foo.由于创建时的某些依赖关系,我无法将h中的结构定义更改为任何其他cpp文件game.so
还有什么其他方法可以解决我在创建时遇到的多定义错误tennis.so?
shared ×11
c ×2
c++ ×2
objective-c ×2
variables ×2
android ×1
attributes ×1
class ×1
constants ×1
convolution ×1
cuda ×1
declaration ×1
erlang ×1
ets ×1
extern ×1
function ×1
gcc ×1
ios ×1
iphone ×1
java ×1
linker ×1
memory ×1
mutex ×1
object ×1
package ×1
perl ×1
pthreads ×1
sapi ×1
singleton ×1
subclass ×1
visual-c++ ×1
windows ×1