- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建 4 个线程以在我的 4 个 CPU 内核上同时运行一个函数。我调用的函数将根据 val
变量值更改一些循环偏移量。
我试过了,但是它没有正确地增加 val
计数器,一些线程报告相同的值,它似乎是随机变化的:
int val = 1;
threads[0] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[1] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[2] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
val++;
threads[3] = CreateThread(0, 0, my_thread_1, &val, 0, 0);
但这似乎工作得很好:
int val1 = 1;
int val2 = 2;
int val3 = 3;
int val4 = 4;
threads[0] = CreateThread(0, 0, my_thread_1, &val1, 0, 0);
threads[1] = CreateThread(0, 0, my_thread_1, &val2, 0, 0);
threads[2] = CreateThread(0, 0, my_thread_1, &val3, 0, 0);
threads[3] = CreateThread(0, 0, my_thread_1, &val4, 0, 0);
这可能是什么原因,如何正确地为线程提供一些参数?
这是我的功能:
DWORD WINAPI my_thread_1(void *params){
int val = *(int *)params;
...
}
最佳答案
失败的原因很简单,因为在您的第一个示例中,您将指针传递给所有 4 个线程的相同内存位置。您在传递指针 之前递增值 的事实是无关紧要的,因为内存位置保持不变。
在您的第二个示例中,您改为将 4 个互斥指针传递给 4 个线程。因此,线程都是读取独立的值。
您可以稍微重组您的代码以帮助提高可维护性和灵 active :
int threadData[NTHREADS]; /* in the future this could be an array of structs */
HANDLE threads[NTHREADS];
int tt;
for (tt = 0; tt < NTHREADS; ++tt)
{
threadData[tt] = tt + 1; /* placeholder for actual logic */
threads[tt] = CreateThread(
NULL, /* lpThreadAttributes */
0, /* dwStackSize */
my_thread_1, /* lpStartAddress */
&threadData[tt], /* lpParameter: each thread will receive its own data */
0, /* dwCreationFlags */
NULL /* lpThreadId */);
}
/* Since threadData and threads are local to the enclosing scope,
* we must wait for them to finish here to ensure we don't read
* data we no longer own
*/
WaitForMultipleObjects(NTHREADS, threads, TRUE, INFINITE);
关于c++ - CreateThread 参数值意外更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8976807/
当我尝试使用CreateThread函数时,我遇到了一个奇怪的错误。这是我的代码: HANDLE threads[3]; //threads[0] is printer, threads[1] is
CreateThread 返回一个HANDLE 对象,需要通过CloseHandle 关闭 文档指出,线程对象保留在系统中,直到线程终止并且通过调用 CloseHandle 关闭线程的所有句柄。 我的
我将 struct 传递给 CreateThread() 函数。另一台机器上的相同代码工作正常。但是在我的机器上,“SendItem”总是变成 0xccccccc Bad Ptr>。有谁知道为什么?
我目前正在从事一个项目,我们使用 pthreads 为 UNIX 系统实现 C 线程。现在我们也希望能够在 Windows 上运行整个项目,我正在翻译 WIN32 的所有线程。现在我遇到了一个问题,我
我在创建数千个线程并关闭它时遇到问题。看一下这段代码: HANDLE threadHandles[i]; for(int i = 0; i < 1000; i++) { CreateThrea
#include DWORD Menuthread(LPVOID in) { return 0; } int main() { CreateThread(NULL, NULL, Menuth
我正在尝试使用 C++ 创建异步服务器监听器我是 C++ 的新手,但我必须做这个项目,我是 Web 开发人员 (PHP),但 PHP 无法建立异步连接+他是一个非常糟糕的大型语言连接量...我可以编写
我正在创建一个线程,它可以控制所有程序过程消息并且不会卡住程序。我写了this code我得到了 program not responding。 有没有人有使用线程的经验可以帮助我? 最佳答案 Win
我正在使用 VC++ 2012 Express 创建一个多线程聊天客户端-服务器。代码改编自此处:http://www.codeproject.com/Articles/14032/Chat-Clie
我想在一个类中启动一个线程,该线程将调用一个函数,该函数是同一类的成员。以下是我要构建的示例代码。 void CM::ExecuteIt() { // Some Calculations if(/*C
这是我第一次与 windows 打交道。我尝试创建线程,但无法编译我的代码。 这是我的代码的一部分: WORD _tid; HANDLE readerThread= CreateThread(0,0
如何给CreateThread回调函数传递int参数?我试了一下: DWORD WINAPI mHandler(LPVOID sId) { ... arr[(int)sId] ... } int id
当我使用CreateThread API方法时,我需要做什么在传递 LPVOID lpParameter 的地方传递多个参数? 最佳答案 您可以创建一个包含所有相关数据的结构,并将指向该结构实例的指针
问题: 如何传递具体两个参数给CreateThread , 何时: 参数一,类型为SOCKET 参数二,一个接口(interface)指针: _COM_SMARTPTR_TYPEDEF(Range,
我只是想知道究竟是什么因素影响了 createthread 的执行速度,以及它必须存活多长时间才能使其“值得”。 背景:我应该在游戏循环中的什么地方生成线程? 最佳答案 主游戏循环不是产生工作线程的地
我正在尝试创建 4 个线程以在我的 4 个 CPU 内核上同时运行一个函数。我调用的函数将根据 val 变量值更改一些循环偏移量。 我试过了,但是它没有正确地增加 val 计数器,一些线程报告相同的值
CreateThread Windows API 函数的保留参数和提交参数有什么区别? 我无法理解以下几行.. The reserve argument sets the amount of addr
我们知道在Windows下创建一个线程的方法有两种,一种就是调用Windows API CreateThread()来创建线程;另外一种就是调用MSVC CRT的函数_beginthread()或_
我在调用 CreateThread 时将类引用作为参数传递给 ThreadProc 时遇到问题。这是一个示例程序,演示了我遇到的问题: program test; {$APPTYPE CONSOLE}
#include #include #include #include #include #include void Thread1( LPVOID param) {
我是一名优秀的程序员,十分优秀!