我在 VS .NET 2003 中的程序存在一些问题。
我最初编写了一个模块,该模块使用 pthread 库创建多个线程来处理某些内容。这在 VS .NET 2003 中正确运行。然后这个模块被其他人使用并集成到另一个更大的程序中。我不确定细节,但该程序创建了一个 GUI,允许用户选择一个选项来运行我的模块。
创建线程时,会传入一个值作为线程 id。我在 GUI 中的模块的问题是线程 id 的值对于所有线程都是 0,而在没有 GUI 的模块中线程 id 是正确的。
以下是在模块中创建线程的方式:
int64_t *tid[1000];
int64_t i = 0, rc;
for (i = 0 ; i < NUM_THREADS ; i++)
{
tid[i] = (int64_t *) malloc(sizeof(int64_t));
*tid[i] = i;
rc = pthread_create(&pthread, &attr, function, (void *)tid[i]);
Sleep(1);
if(rc)
{
free(tid[i]);
exit(1);
}
free(tid[i]);
}
我检查了两者的项目属性,下面列出了两个项目之间的唯一区别:
GUI - use managed extensions | my module (w/o GUI) - does not use managed extensions
In C/C++ preprocessor:
GUI - WIN32;_DEBUG;_CONSOLE;WINDOWS | my module (w/o GUI) - none
In C/C++ Additional Options:
GUI - /CLR | my module (w/o GUI) - no /CLR (error with /CLR: fatal error LNK1000: Internal error during BuildImage)
代码是一样的,所以我不明白为什么 GUI 的输出是错误的,除非使用托管扩展/clr 会有所不同?(我也不确定那些是什么。)
编辑添加输出线程 id 的代码部分:
void *function(void *input)
{
int64_t threadid = *(int64_t *)input;
printf("threadid = %ld\n", threadid);
...
}
请指教。
谢谢你。
问候,雷恩