易截截图软件、单文件、免安装、纯绿色、仅160KB

C runtime lib 线程安全

周五开会做团队成员codereivew, 有同事提出了一个关于gmtime多线程是否安全的问题, 当时觉得程序Link了VC的多线程库,应该不是问题。还好回头核实了一下,发现了竟是一个潜在的bug。
程序分在application(完成项目特定功能)和framework(可重用的核心功能)两部分,线程的启动由framework完成。 app中使用了gmtime/localtime等C库函数。
gmtime/localtime等C函数是非线程安全的,这个n年前就知道, 也知道在Windows平台上VC有MT版本的runtime library,通过线程的局部存储(TLS)实现了/mt 链接时的线程安全。 然而线程的启动必须通过C运行时库的_beginthreadex, 而不是win32 API CreateThead 。不幸的的是当年framework的实现者貌似不清楚_beginthreadex与CreateThread的区别(BTW, 这个是当年进入第一家公司的一道面试题,所以印象颇为深刻),直接调CreateThread启动了线程。
解决方案调查中,VC CRT竟是没有gmtime_r/localtime_r。
参考资料:
http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/c727ae29-5a7a-42b6-ad0b-f6b21c1180b2
http://www.tech-archive.net/Archive/VC/microsoft.public.vc.language/2006-02/msg00013.html


相关文档:

C语言编写linux下的守护进程

利用
下载的这段代码,成功实现了守护进程,原来守护进程是很简单的事情。
  在main函数中执行
  init_daemon();//初始化为Daemon
  就可以把进程变成守护进程
  
#include
#include
#include
#include
#include
void
 init_daemon(void
)
{
int
 pid;
int
 i;
if
(pid=fork()) ......

C/C++中的64位整数(__int64 and long long)

  在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int
范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即
0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。
  那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展 ......

C。。。。越来越和谐了啊。。= =

输入一个英文语句(不记标点符号,单词之间只有一个空格),再逆向输出
例如:I am game boy
         boy game am I
#include<stdio.h>
#include<string.h>
int f(char,char,int,int);
int main()
{
char a[80]={0},b[80]={0},c[80]={0};
int i,n,m=0;
......

memcachde 与 c/c++ 应用

修改makefile,在LIBS里面加上-lmemcached,比如原来 gcc test.c,现在 gcc test.c -lmemcached。这个库就是libmemcached提供的。
然后添加#include<libmemcached/memcached.h>,这个文件也是libmemcached提供的。
主函数里面需要添加:
    memcached_st *memc;
    uint32_t&nbs ......

c指针

c指针的运算有时候还是很迷惑人的。
例如:
struct student {
int num;
int score;
int length;
};
struct student *pt;
pt = (struct student *) malloc(sizeof(struct student));
pt->num = 1;
pt->score = 90;
pt->length = 3 * sizeof(int);
printf("pt length:%d\n", *pt);
pt = (int ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号