我试图找到一种在运行时编译程序集并加载它们的方法.基本意图是将它们存储在不在光盘上的数据库中.所以我写了一些代码,但看到了一个有趣的情况.这是我的代码:
//SumLib
namespace SumLib
{
public class SumClass
{
public static int Sum(int a, int b)
{
return a + b;
}
}
}
// Console app
class Program
{
public static void AssemblyLoadEvent(object sender, AssemblyLoadEventArgs args)
{
object[] tt = { 3, 6 };
Type typ = args.LoadedAssembly.GetType("SumLib.SumClass");
MethodInfo minfo = typ.GetMethod("Sum");
int x = (int)minfo.Invoke(null, tt);
Console.WriteLine(x);
}
static void Main(string[] args)
{
AppDomain apd = AppDomain.CreateDomain("newdomain", AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation);
apd.AssemblyLoad += new AssemblyLoadEventHandler(AssemblyLoadEvent);
FileStream fs = new FileStream("Sumlib.dll", …Run Code Online (Sandbox Code Playgroud) 让我们说:
public interface IBase
{
// Stuff
}
public class Derived : IBase
{
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
做的时候
Derived instance_ = new CDrv();
Ibase ibase = instance_; // Line 1
Run Code Online (Sandbox Code Playgroud)
这是隐式转换还是赋值?
是否可以通过重写转换或赋值运算符或其他方法来生成克隆instance_和赋值/转换ibase?
或者换句话说,是否有任何方法可以传递instance_by值而不是使其成为struct?我不希望它是一个结构,因为我有许多函数返回Derived,上面的转换/赋值很少发生.
我想将TCMalloc与STL容器一起使用,所以我需要一个用TCMalloc构建的分配器(就像带有TBB malloc的tbb_allocator).我找不到任何TCMalloc文档(如果它被称为文档).所以我开始探索头文件并找到一个名为的类STL_Allocator.但有些事情对我来说并不清楚.来自stl_allocator.h的报价:
// Generic allocator class for STL objects
// that uses a given type-less allocator Alloc, which must provide:
// static void* Alloc::Allocate(size_t size);
// static void Alloc::Free(void* ptr, size_t size);
//
// STL_Allocator<T, MyAlloc> provides the same thread-safety
// guarantees as MyAlloc.
//
// Usage example:
// set<T, less<T>, STL_Allocator<T, MyAlloc> > my_set;
// CAVEAT: Parts of the code below are probably specific
// to the STL version(s) we are using.
// The code …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序项目,管理和非托管代码都运行,我需要使用相同的算法在两个系统中散列双值.所以要么我将覆盖System.Double.GetHashCode()或在c ++代码中使用其算法.我找不到double.gethashcode算法并决定覆盖该函数.但我有一个奇怪的错误.
无法将类型double隐式转换为System.Double
这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System
{
public struct Double
{
unsafe public override int GetHashCode()
{
fixed (Double* dd = &this)
{
int* xx = (int*)dd;
return xx[0] ^ xx[1] ;
}
}
}
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double dd = 123.3444; // line 1
// Double dd = 123.3444; // line 2
// Cannot implicitly convert type double to System.Double
Console.WriteLine(dd.GetHashCode());
Console.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我需要在单独的程序集中部分定义一个类.实际上我需要部分重新定义一个类,它已经在用C++ Cli编写的程序集中定义,但这可能是一个不同的问题.
对于这种情况,所有用c#编写的代码,我在basenamespace程序集中都有一个基类定义
using System;
namespace BaseNameSpace
{
public class BaseClass
{
public int Num;
public double dNum;
public BaseClass(int s, double d)
{
Num = s;
dNum = d;
}
public virtual void Wrt()
{
Console.WriteLine("{0},{1}", Num, dNum);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加另一个名为derivedclassSpace的程序集项目,并声明从baseclass派生的derivedclass.我还为此项目添加了一个部分类定义.
using System;
using BaseNameSpace;
namespace BaseNameSpace
{
public partial class BaseClass
{
public void Mult()
{
Num *= 2;
}
}
}
namespace DerivedNameSpace
{
public class DerivedClass : BaseClass
{
public DerivedClass(int s)
: base(s, 0) …Run Code Online (Sandbox Code Playgroud) 我有一个完全线程安全的FIFO结构(TaskList)来存储任务类,多个线程,其中一些创建和存储任务,其他线程处理任务.TaskListclass有一个pop_front()方法,如果至少有一个,则返回第一个任务.否则它会返回NULL.
这是一个处理函数的例子:
TaskList tlist;
unsigned _stdcall ThreadFunction(void * qwe)
{
Task * task;
while(!WorkIsOver) // a global bool to end all threads.
{
while(task = tlist.pop_front())
{
// process Task
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有时候,任务列表中没有新任务,因此处理线程进入无限循环(while(!WorkIsOver))并且CPU负载增加.不知何故,我必须让线程等待,直到新任务存储在列表中.我考虑暂停和恢复,但后来我需要关于哪些线程挂起或运行的额外信息,这给编码带来了更大的复杂性.
有任何想法吗?
PS.我使用的是winapi,而不是Boost或TBB用于线程化.因为有时我必须终止处理时间过长的线程,并立即创建新的线程.这对我来说至关重要.请不要建议这两个中的任何一个.
谢谢
我在我的应用程序中有这段代码.我怀疑它不是线程安全的,所以决定问问SOers.
int * volatile int_ptr;
int count;
Run Code Online (Sandbox Code Playgroud)
线程1:
void grow(int new_count)
{
if(new_count <= count) return;
int * new_ptr = new int[new_count];
memset(new_ptr, 0 , new_count * sizeof(int));
memcpy(new_ptr,int_ptr,count);
int * dum_ptr = (int *)InterlockedExchange((volatile long *)&int_ptr,(long)new_ptr)
count = new_count;
delete [] dum_ptr;
}
Run Code Online (Sandbox Code Playgroud)
线程2:
int get_value(int index)
{
return int_ptr[index];
}
Run Code Online (Sandbox Code Playgroud)
我知道可以使用CRITICAL_SECTION,但是线程1可以在一周内工作一次,而线程2可以在一天内工作数百万次.在99.99999%的尝试中int_ptr,第二个线程将进入和退出临界区.这对我来说没有意义.该应用程序仅适用于Windows 2000及更高版本,其中英特尔处理器显然具有多核.
这段代码是线程安全的吗?如果没有,我该怎么做才能使它具有线程安全性?我怎么能原子地读取int_ptr?即:
int * dummy_ptr = read_atomic<int *>(int_ptr);
return dummy_ptr[index];
Run Code Online (Sandbox Code Playgroud)
解决方案,包括std::vector,让我更快乐,更舒适.
在下面的代码中,当我将一个未命名的A变量传递给ctor时B,该变量在该行之后被破坏.根据这个答案:
临时对象在它们所属的完整表达结束时被销毁.完整表达式是不是某个其他表达式的子表达式的表达式.通常这意味着它结束于
; (or ) for if, while, switch etc.)表示语句的结尾.
我明白了,但是如果它被破坏了,那么这个类怎么能B知道它mamber_a变量的值呢?我知道复制的ctor A是enver.这怎么可能?
#include <iostream>
using namespace std;
class A
{
int sign;
A();
const A & operator=(const A &);
public:
A(int x) : sign(x) {
cout << "A ctor : " << sign << endl;
}
void WriteA() const {
cout << sign << endl;
}
~A() {
cout << "A dtor : " << sign …Run Code Online (Sandbox Code Playgroud)