e***n 发帖数: 286 | 1 我有这样一段程序:
// Define 10 subproblems of the same content
DWORD WINAPI sub1(LPVOID lpPara);
DWORD WINAPI sub2(LPVOID lpPara);
...
DWORD WINAPI sub10(LPVOID lpPara);
// Define function pointer array
DWORD (WINAPI *PtrSub[10])(LPVOID = {sub1, sub2, ..., sub10};
// Start 10 multithreads
HANDLE hThread[10];
for(int ix = 0; ix < 10; ++ix)
hThread[ix] = CreateThread(NULL, 0, PtrSub[ix], NULL, 0, NULL);
for(int ix = 0; ix < 10; ++ix)
CloseHandle(hThread[ix]);
//...
while(SOLVED) Sleep(50);
//...
。。。 | c********e 发帖数: 383 | 2 the reason for the interface mismatch is that a normal member function has
a implicit augment -> this pointer passed in, and the way using object array
instead of function pointer array virtually does not performance
enhancement.
btw, if u are doing c programming why would you want to do object?
if your sub1-10 are the same, you can just define one sub function for all
threads to use, however, you can pass in different augments into the sub
when you
create threads
【在 e***n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 我有这样一段程序: : // Define 10 subproblems of the same content : DWORD WINAPI sub1(LPVOID lpPara); : DWORD WINAPI sub2(LPVOID lpPara); : ... : DWORD WINAPI sub10(LPVOID lpPara); : // Define function pointer array : DWORD (WINAPI *PtrSub[10])(LPVOID = {sub1, sub2, ..., sub10}; : // Start 10 multithreads : HANDLE hThread[10];
| t****t 发帖数: 6806 | 3 干嘛不在sub里自己开数据结构?
array
【在 c********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : the reason for the interface mismatch is that a normal member function has : a implicit augment -> this pointer passed in, and the way using object array : instead of function pointer array virtually does not performance : enhancement. : btw, if u are doing c programming why would you want to do object? : if your sub1-10 are the same, you can just define one sub function for all : threads to use, however, you can pass in different augments into the sub : when you : create threads
| c********e 发帖数: 383 | 4 我觉得他现在就是怎么干的,但是他copy了9次那个func或者聪明一点9次那个func
pointer.
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 干嘛不在sub里自己开数据结构? : : array
| t****t 发帖数: 6806 | 5 你真闲啊
【在 c********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 我觉得他现在就是怎么干的,但是他copy了9次那个func或者聪明一点9次那个func : pointer.
| c********e 发帖数: 383 | 6 坐在地板上,电脑屏幕也在地板上...过两天也没床了
更没有wow玩,
能不闲么
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 你真闲啊
| t****t 发帖数: 6806 | 7 啥时候开始上班?
【在 c********e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 坐在地板上,电脑屏幕也在地板上...过两天也没床了 : 更没有wow玩, : 能不闲么
| c********e 发帖数: 383 | 8 h1b办不下来我也没办法,还没辞职呢,天天磨洋工
【在 t****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 啥时候开始上班?
| e***n 发帖数: 286 | 9 终于搞定了, 其实就是定义一个 sub 就行了, 开多个线程调用, 重新把数据结构整理
了一下, 把公用的数据和本线程内局部数据分清楚就行了, 第一次用多线程, 觉得还真
是不错. 我这是个搜索算法, 开 2 个线程后用了个很简单的规则来交换线程间信息,
感觉多线程对搜索算法效率是有所提高, 但是开 3 个线程以上的性能提高就没那么显
著了.
【在 e***n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 我有这样一段程序: : // Define 10 subproblems of the same content : DWORD WINAPI sub1(LPVOID lpPara); : DWORD WINAPI sub2(LPVOID lpPara); : ... : DWORD WINAPI sub10(LPVOID lpPara); : // Define function pointer array : DWORD (WINAPI *PtrSub[10])(LPVOID = {sub1, sub2, ..., sub10}; : // Start 10 multithreads : HANDLE hThread[10];
| a**u 发帖数: 5 | 10 playing with multi-thread programming is interesting....
but in terms of searching... only if you have long waiting time in one
thread, such as wait for internet response, otherwise you will have overhead
when using multi-thread. IMHO | e******r 发帖数: 220 | 11 which compiler did you use for this thing? Thanks
BTW: MICROSOFT VISUAL STUDIO sucks, I even couldn't see race condition when
I test the code and expect to see race condition.
【在 e***n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 我有这样一段程序: : // Define 10 subproblems of the same content : DWORD WINAPI sub1(LPVOID lpPara); : DWORD WINAPI sub2(LPVOID lpPara); : ... : DWORD WINAPI sub10(LPVOID lpPara); : // Define function pointer array : DWORD (WINAPI *PtrSub[10])(LPVOID = {sub1, sub2, ..., sub10}; : // Start 10 multithreads : HANDLE hThread[10];
|
|