2
我正在開發一個C++多線程應用程序,並面臨與多線程程序中成員變量相關的幾個問題。C++多線程成員變量
就我所知,每個線程的堆是常見的,它存在於全局區域中,而每個線程都有自己的堆棧。請建議如何在以下情況下共享成員變量:
- 哪個對象的類是單例,內存分配是動態的。對象是成員變量。
代碼示例:
class A
{
// SomeRandomClass is Singleton class
SomeRandomClass* someRandomClass::m_pInstance = NULL;
main()
{
pthread_t thread1;
pthread_create(&thread1,NULL,func,NULL)
}
func()
{
// Dynamic allocation and object is of singleton class
myObject = SomeRandomClass::instance();
}
}
-
其中
- 類是由目的是常規類和存儲器分配是動態的。對象是成員變量。
類其目的是由單件是和內存分配是靜態的。對象是成員變量。
哪個對象的類是普通類,內存分配是靜態的。對象是成員變量。
代碼示例:
class A
{
// SomeRandom Class is not singleton
SomeRandomClass* m_pInstance = NULL;
main()
{
pthread_t thread1;
pthread_create(&thread1,NULL,func,NULL)
}
func()
{
// Dynamic allocation and object is of singleton class
myObject = new SomeRandomClass();
}
}
請在這裏幫忙,因爲我在網上閱讀各種文章,我無法得到的概念。
這聽起來像作業。 –
首先它不是。但是,如果我明確了我的概念,即使這是問題所在。 –
就是這樣。問題不清楚。如果你展示了一些示例代碼,它可能會更清晰。你的意思是「內存分配是靜態的」? –