简体   繁体   English

在VS .NET 2003中使用pthread

[英]Using pthreads in VS .NET 2003

I'm having some problems with my program in VS .NET 2003. 我在VS .NET 2003中的程序遇到一些问题。

I initially wrote a module that uses the pthread library to create a number of threads to process something. 我最初编写了一个模块,该模块使用pthread库创建许多线程来处理某些事情。 This runs correctly in VS .NET 2003. Then this module was used by someone else and integrated into another larger program. 这可以在VS .NET 2003中正确运行。然后该模块被其他人使用,并集成到另一个更大的程序中。 I'm not sure about the details, but the program creates a GUI that allows the user to select an option to run my module. 我不确定细节,但是程序创建了一个GUI,允许用户选择一个选项来运行我的模块。

When the thread is created, a value is passed in as the thread id. 创建线程时,将传入一个值作为线程ID。 The problem with my module in the GUI is that the value of the thread id is 0 for all the threads, while the thread id is correct in the module without GUI. 我的模块在GUI中的问题是所有线程的线程ID的值为0,而在没有GUI的模块中线程ID是正确的。

Here's how the thread is created in the module: 这是在模块中创建线程的方式:

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]);
}

I checked the project properties of both, and the only differences between the 2 projects are listed below: 我检查了两个项目的属性,下面列出了两个项目之间的唯一区别:

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)

The code is the same, so I don't get why the output is wrong for the GUI, unless the use of managed extensions/clr somehow makes a difference? 代码是一样的,所以我不明白为什么GUI的输出是错误的,除非使用托管扩展/ clr有所不同? (I'm not really sure what those are either.) (我也不是很清楚。)

Edited to add the part of code that outputs the thread id: 编辑添加了输出线程ID的代码部分:

void *function(void *input)
{
   int64_t threadid = *(int64_t *)input;
   printf("threadid = %ld\n", threadid);
   ...
}

Please advise. 请指教。

Thank you. 谢谢。

Regards, Rayne 问候,雷恩

It appears you have a race condition when passing the tid to the function - try removing the Sleep(1) and having the thread free tid instead. 将tid传递给函数时,您似乎处于竞争状态-尝试删除Sleep(1)并使用无线程tid。 From your comment above, this seems to solve the issue. 从上面的评论来看,这似乎可以解决问题。

To answer your other question, when pthread_create returns to your main thread, the new thread has been created (memory allocated for thread in OS, etc) but may not have actually run yet. 为了回答您的另一个问题,当pthread_create返回到主线程时,已经创建了新线程(已为OS中的线程分配了内存等),但实际上可能尚未运行。 Putting Sleep(1) makes it more likely that the new thread will run (forces main thread to give up timeslice) but there's no guarantee thread will get tid before main thread runs again and calls free(tid[i]); 放置Sleep(1)使得新线程更有可能运行(强制主线程放弃时间片),但是不能保证线程会在主线程再次运行并调用free(tid [i])之前获得tid。

Hence the idea of having the worker thread free tid - it will free after it has picked it up. 因此,使工作线程空闲tid的想法-拾取它后将释放。

If you change your tid array to an array of int64's and pass &tid[i] then you won't need malloc/free and the race condition is also solved - though this only works if you're guaranteed to have only one caller to your library at once. 如果将tid数组更改为int64的数组并传递&tid [i],则将不需要malloc / free并且竞争条件也已解决-尽管只有在保证只有一个调用方的情况下,此方法才有效一次图书馆。

I hope this helps - getting threading right can be tricky but well worth the effort! 我希望这会有所帮助-正确执行线程可能会很棘手,但值得付出努力!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM