- 论坛徽章:
- 0
|
//CxxTst.cpp
#include <stdio.h>;
#include <stdlib.h>;
#include <pthread.h>;
template <class Type>; class CMyData
{
public:
Type Get()
{
Type t;
pthread_mutex_lock(&lock);
t = m_T;
pthread_mutex_unlock(&lock);
return t;
}
void Set(Type t)
{
pthread_mutex_lock(&lock);
m_T = t;
pthread_mutex_unlock(&lock);
return;
}
public:
CMyData()
{
pthread_mutex_init(&lock);
}
~CMyData() {};
private:
pthread_mutex_t lock;
Type m_T;
};
void main()
{
CMyData<int>; var;
var.Set(100);
var.Get();
return;
}
//////////////////////////////////////////////////////////
Here is my complitation result on a sun workstation. but Why????????
who can tell me How to get an thread-safe template class.?????
sundemo>;CC -o xxx CxxTst.cpp
"CxxTst.cpp", line 44: Warning (Anachronism): main() must have a return type of int.
"CxxTst.cpp", line 32: Error: Too few arguments in call to "pthread_mutex_init(_pthread_mutex*, const _pthread_mutexattr*)".
"CxxTst.cpp", line 45: Where: While instantiating "CMyData<int>;::CMyData()".
"CxxTst.cpp", line 45: Where: Instantiated from non-template code. |
|