两个线程可以使用相同的线程程序吗? [英] Can two Threads use same Thread Procedure?

查看:171
本文介绍了两个线程可以使用相同的线程程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用CreateThread()时,两个线程可以使用单个函数ThreadProc作为其线程过程。

s it possible for two threads to use a single function "ThreadProc" as its thread procedure when CreateThread() is used.

HANDLE thread1= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
                              (LPVOID) &i, //Thread parameter
                              0, //Immediately run the thread
                              &dwThreadId //Thread Id
                            ) 
HANDLE thread2= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
                              (LPVOID) &i, //Thread parameter
                              0, //Immediately run the thread
                              &dwThreadId //Thread Id
                            )





上面的代码会创建两个线程,每个线程具有相同的功能(因为两个线程的线程程序是相同的。)我这样做了吗?



如果可能,那么会出现任何同步问题,因为两个线程都使用相同的线程程序。



请帮忙我这个。我真的很困惑,在互联网上找不到任何东西。



Would the above code create two threads each with same functionality(since thread procedure for both of the threads is same.) Am I doing it correctly?

If it is possible then would there be any synchronization issues since both threads are using same Thread Procedure.

Please help me with this. I am really confused and could not find anything over the internet.

推荐答案

这取决于。

哪个不太有帮助,但是是真的。是的,两个独立的线程可以执行相同的代码,这不是问题。它们所处理的数据可能会导致问题。如果他们都尝试在程序之外使用相同的变量那么确实会导致问题,并且你需要处理锁定等等以确保它的工作(这包括作为参数传入的内存。如果它使用的所有内存都是本地分配给函数(即作为堆栈上的本地非静态变量,或在函数内的堆上分配)然后它应该工作正常。谷歌的线程安全,你应该找到你所需要的一切。
It depends.
Which isn''t too helpful, but is true. Yes, two separate thread can execute the same code, that isn''t a problem. It''s the data that they work on that may cause problems. If they both try to use the same variable outside the procedure then that can indeed cause problems, and you need to handle locking and so forth to ensure it works (and this includes memory passed in as a parameter. If all the memory it uses is allocated locally to the function (i.e. as local non-static variables on the stack, or allocated on the heap within the function) then it should work fine. Google for "Thread safety" and you should find all you need.


Quote:

上面的代码是否会创建两个具有相同功能的线程(因为两个线程的线程都是相同。)

Would the above code create two threads each with same functionality(since thread procedure for both of the threads is same.)

是的。



Yes.

引用:

我在做什么它是否正确?

Am I doing it correctly?

基本上是的。但是你不应该传递 i 的地址,如果它是一个局部变量(可能你只需要它的值)。 br />
更改

Basically yes. However you shouldn''t pass address of i if it is a local variable (and probably you just need its value).
Change

(LPVOID) &i




to

(LPVOID) i



此外,我不会覆盖 dwThreadId 值,使用两个变量就像你为螺纹手柄所做的那样。






Moreover, I wouldn''t overwrite dwThreadId value, use two variables like you did for the thread handles.


Quote:

如果有可能则会出现任何同步问题,因为两个线程都使用相同的线程过程。

If it is possible then would there be any synchronization issues since both threads are using same Thread Procedure.



当然存在同步问题,但是它们不依赖于线程共享相同的功能(每个线程都有自己的局部变量副本)。它们依赖于允许两个线程访问全局变量。因此,两个线程之间以及每个线程与主线程之间存在同步问题。


Of course there are synchronization issues, however they do not depend on thread sharing the same function (each thread have its own copy of local variables). They depend on both threads being allowed to access global variables. So there are synchronization issues between the two threads and between each of them and the main one.


这篇关于两个线程可以使用相同的线程程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆