English 中文(简体)
穿透结构
原标题:Passing structure to thread using _beginthreadx
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <windows.h>
#include <process.h>

using namespace std;
HANDLE ghEvents;


struct DataStructure
{
int r[2];
int h;
};


unsigned __stdcall f2(void *p)
{

DataStructure *input = (DataStructure *)p;
int i = (int)input->h;
cout <<i<<endl;

}   


int main()
{

HANDLE hThread[8];
DWORD i, dwEvent, dwThreadID;
unsigned threadID;
DataStructure input;

ghEvents = CreateEvent(NULL,FALSE,FALSE,NULL);  

for (int i = 0; i < 8; i++) 
{   
input.h=i;
hThread[i] = (HANDLE)_beginthreadex( NULL, 0, f2, (void*)&input, 0, &threadID );
}

dwEvent = WaitForMultipleObjects(8,hThread,TRUE,INFINITE);       

CloseHandle(ghEvents); 
for (int i = 0; i < 8; i++) 
{
CloseHandle( hThread[i] );
}

cin.get();
return 0;

}

产出为7777777777,而不是12345678。

我知道,我不得不以价值而不是参照的方式通过意见,但我却不断向我发出错误信息,这是这样做的适当方式?

最佳回答

这是在我先前的答复之后,如果在汇编时间时知道校对的数量,那么解决办法就会更好。

DataStructure input[8];  

... 

for (int i = 0; i < 8; i++)   
{     
   input[i].h=i;  
   hThread[i] = (HANDLE)_beginthreadex( NULL, 0, f2, (void*)&input[i], 0, &threadID );  
} 

你们需要回馈一个价值:

unsigned __stdcall f2(void *p)      
{      

   DataStructure *input = (DataStructure *)p;      
   int i = input->h;      
   cout <<i<<endl;      
   return 0; 
} 
问题回答

页: 1 你的产出不是决定性的。 视每个校对何时运行而定,可在该校前后阅读。 在穿透和进入<代码>input->h时,主线可能已经继续并修改了input.h

例:

  • Main - Loop iteration 0 sets input.h to 0.
  • Main - Loop iteration 0 starts Thread 0 with input as a parameter.
  • Main - Loop iteration 1 sets input.h to 1.
  • Main - Loop iteration 1 starts Thread 1 with input as a parameter.
  • Main - Loop iteration 2 sets input.h to 2.
  • Main - Loop iteration 2 starts Thread 2 with input as a parameter.
  • Thread 1 - Starts up.
  • Thread 0 - Starts up.
  • Main - Loop iteration 3 sets input.h to 3.
  • Thread 0 - Reads input->h as 3.
  • Thread 2 - Starts up.
  • Main - Loop iteration 3 starts Thread 3 with input as a parameter.
  • Thread 1 - Reads input->h as 3.
  • Thread 3 - Starts up.
  • Main - Loop iteration 4 sets input.h to 4.
  • Thread 3 - Reads input->h as 4.
  • Thread 2 - Reads input->h as 4.
  • Thread 4 - Starts up.
  • Thread 4 - Reads input->h as 4.

最后产出:3344

每个校对不同的<代码>DataStructure,以便它们不会试图从同一个记忆地址读到。 这样就没有种族关系。 该术语指的是,没有保障read起和执行秩序,因此,如果read子在没有同步的情况下获得同样的资源,就会“感到羞耻”。

第一点是:

您在数据结构方面没有任何协调,而数据结构正传给多个线索,虽然这些线索正在做一些事情,但你已经通过下一个通道和改变贵数据结构的价值。

在坡道内建立新的数据结构,以避免出现同步问题。

你们需要为每一个线索建立数据结构,因为你在为每一线透镜撰写投入的价值。

So to fix, change it to

DataStructure *input; 

...

for (int i = 0; i < 8; i++)  
{    
   input = new DataStructure ;
   input->h=i; 
   hThread[i] = (HANDLE)_beginthreadex( NULL, 0, f2, (void*)input, 0, &threadID ); 
}

And to avoid a memory leak get the f2 function to delete the input i.e.

unsigned __stdcall f2(void *p)     
{     

   DataStructure *input = (DataStructure *)p;     
   int i = input->h;     
   cout <<i<<endl;     
   delete input;
   return 0;
}

<<>>>> 如果在汇编时间时没有校对面数目,那么这种解决办法使用动态记忆分配办法,也是好的解决方案。 如果知道胎面的数量,请见我的另一种答案。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签