Category Archives: 技术

基于windows线程的并行前缀求和

#include #include #include #include #define NUM_THREADS 4 int N = 10000, *A; int intTotals[NUM_THREADS], outTotals[NUM_THREADS]; HANDLE doneStep1[NUM_THREADS]; HANDLE doneStep2; unsigned __stdcall prefixScan(LPVOID pArg) { if(NULL != pArg) { int tNum = *((int*)pArg); int start, end, i; int lPrefixTotal; free(pArg); pArg … Continue reading

Leave a Comment

Filed under C++, 技术

基于windows线程的并行线性查找

#include #include #include #include typedef struct{ int * A; int num; int key; int threadID; } sParam; bool Done = FALSE; #define NUM_THREADS 4 void linearSearch(int * A, int s, int e, int key, DWORD *position) { int i; for … Continue reading

Leave a Comment

Filed under C++, 技术

二进制文件二分查找算法

package com.xxx.xxx.query.version2; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.xxx.xxx.decode.MetaData; import com.xxx.xxx.utils.DateFormatUtil; public abstract class DataReader { protected static Log log = LogFactory.getLog(DataReader.class); protected static final int BUF_SIZE … Continue reading

Leave a Comment

Filed under C++, 技术

the art of c++第一章垃圾回收器iterator dereferencable错误解决方法

昨天看《the art of cpp》的时候,第二章垃圾回收器的sample代码 gc.h 在执行垃圾回收函数的时候 总是报iterator dereferencable的错误。 今天调试了一下,发现书上的源代码中 for(p = gclist.begin(); p != gclist.end(); p++) { … // Remove unused entry from gclist. gclist.remove (*p); … } 查了一下stl的reference,发现是的问题list 的remove 方法使用不当导致的iterator dereferencable 执行报错; 改为 for(p = gclist.begin(); p != gclist.end(); … Continue reading

3 Comments

Filed under C++, 技术

TCP/IP编程的几种有趣的模式

1 使用原始IP数据包,避免针对TCP数据包的过滤 windows xp 和 windows server提供了直接针对原始数据包编程的接口,我们可以在此基础上设计自己的类TCP和UDP协议。 例如我们开发后台监控程序或者希望能够穿透针对TCP数据包的过滤的防火墙。 2 发送数据包和接收数据包使用不同线程,提高网络吞吐量 使用TCP协议,客户端和服务端难以避免需要进行三次握手,数据传输过程中还需要进行确认。这些通信过程都是同步的。 虽然我们可以使用多线程,但是仍面临windows系统最大线程数的限制。 因此对于许多类似于扫描端口,扫描ftp目录内容,搜索内容等连接并发数比较大的任务。一种合理的解决方案是使用一个发送线程发送请求数据包。使用一个接收线程监听某一特定端口监听,接收数据包。 值得注意的是如果直接使用Windows的TCP协议包,发送TCP数据包后线程会进入堵塞状态,因此我们要使用原始的IP数据包实现自定义的TCP数据包,构建一个TCP数据包,修改TCP数据包的首部。把源端口设置为接收线程监听的端口。发送数据之后随即断开连接,处理下一个请求。 TCP数据包的首部结构如下: typedef struct tcphdr //定义TCP首部 { USHORT th_sport; //16位源端口 USHORT th_dport; //16位目的端口 UINT th_seq; //32位序列号 UINT th_ack; //32位确认号 UCHAR th_lenres; //4位首部长度/6位保留字 UCHAR th_flag; //6位标志位 USHORT th_win; … Continue reading

Leave a Comment

Filed under 技术

begin with pcap

最近因为受了某些启发开始研究pcap 今天看了一些WpdPack的sample代码 计划周末做一个“单向代理模式”的demo。 鲜花总会长出来,不在墙这边相见,就在墙外面思念。-和菜头

Leave a Comment

Filed under 技术

指针携带额外信息

我们有这样两个方法 PostQueuedCompletionStatus 和 GetQueuedCompletionStatus : BOOL PostQueuedCompletionStatus( HANDLE CompletionPort, // handle to an I/O completion port DWORD dwNumberOfBytesTransferred, // value to return via // GetQueuedCompletionStatus’ // lpNumberOfBytesTranferred DWORD dwCompletionKey, // value to return via // GetQueuedCompletionStatus’ // lpCompletionKey LPOVERLAPPED lpOverlapped … Continue reading

Leave a Comment

Filed under C++, 技术

C++编译器自动生成的基本函数

1 编译器自动生成的基本函数 C++编译器会在开发人员没有声明下列函数的时候,声明编译器自己的版本。 class Empty{} 等效于下面的声明 class Empty{ public: Empty(); //缺省构造函数 Empty(const Empty& rhs); //拷贝构造函数 ~Empty(); //析构函数 Empty & operator=(const Empty& rhs); //赋值运算符 Empty & operator&(); //取址运算符 const Empty * operator&() const; } 下面是编译器的参考实现 inline Empty::Empty() {} inline Empty::~Empty() {} … Continue reading

2 Comments

Filed under C++, 技术

C++ virtual应用的约束

virtual不正确使用会破坏数据的完整性(初始化完整性和释放完整性)。 delete或者free的时候需要指定类型,否则会造成部分资源未能被成功释放。 析构函数中不要调用虚函数,因为不会调用派生类对应的虚函数实现。 派生类的析构函数必须声明为virtual,否则可能不会调用派生类的析构函数。 C++标准规定:当通过基类的指针去删除派生类的对象,而基类又没有析构函数的时候,结果是不可确定的。 类对象实例含有虚函数,会导致对象实例的体积增加,因为持有一个指向虚函数表的指针。

Leave a Comment

Filed under C++, 技术