我想使用 pthread 实现一个线程类。 当然,我希望我创建的每个线程都有不同的启动例程。 pthread_create 只允许一个静态函数作为起始例程,所以它不能被实例化。 有没有办法允许这样做,还是使用结构来处理我的线程更好? 这是我到目前为止写的代码:
class thread {
string name;
pthread_t id;
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_attr_t attr;
public:
thread (string t_name);
static void* start(void*);
int id_get();
private:
};
thread::thread (string t_name)
{
name = t_name;
pthread_attr_init(&attr);
int stacksize = sizeof(double) * TH_STACK_SIZE * 30;
pthread_attr_setstacksize(&attr, stacksize);
int rc = pthread_create (&id, &attr, &start, NULL);
cout << "return_code: " << rc << endl;
cout << id;
}
void* thread::start(void*)
{
while(1){
cout << "here";
pthread_exit(NULL);
}
}
int thread::id_get()
{
return id;
}
我的测试主要是:
int main(void) {
cout << "Creating threads" << endl;
thread test1("first");
thread test2("second");
pthread_join(test1.id_get(),NULL);
pthread_join(test2.id_get(),NULL);
return 0;
}
最佳答案
I would like to have different starting routines for each thread I'm creating.
当我使用 posix 线程时(我现在使用 std::thread),我使用了“两步”进入机制。以这两个步骤的(小)成本,每个类都可以轻松拥有自己的线程。
我总是将这些入口方法保密。
class Foo_t
{
// ... etc
private:
static void* threadEntry(void* ptr);
void* threadEntry2(void); // thread actions in an object method
// ... etc
}
因为这些是私有(private)的,该类有一些公共(public)方法来创建 posix 线程,通常是这样的:
void Foo_t::startApp()
{
// ... etc
int pcStat = m_Thread.create(Foo_t::threadEntry, this);
// this 2 parameter method of my thread wrapper class
// invoked the 4 parameter "::pthread_create(...)".
// The 'this' param is passed into the 4th parameter, called arg.
dtbAssert(0 == pcStat)(m_nodeId)(pcStat)(errno);
// ...
}
请注意 m_Thread.create() 的第二个参数“this”。
线程将在静态方法中启动:
void* Foo_t::threadEntry(void* a_ptr)
{
dtbAssert(a_ptr != 0);
Foo_t* a_foo = static_cast<Foo_t*>(a_ptr);
void* retVal = a_foo->threadEntry2();
return(retVal);
这里,void*参数填充了类实例的'this'指针,然后static_cast返回我们需要的,一个Foo_t*。请记住,此方法是私有(private)的,因此只有 startApp() 会创建一个线程。
注意 threadEntry() 调用类实例的一个实际方法:
void* Foo_t::threadEntry2(void)
{
DBG("Thread %2d (id=%lx): sems %p/%p, "
"Entering sem controlled critical region\n", ...);
// ... start thread work
}
从这里开始,实例的任何方法都可用。
那么,接下来呢。有很多方法可以继续执行不同的线程例程。
考虑向 startApp 添加一个参数:
void Foo_t::startApp(int select);
“int select”和 switch/case 语句可以运行唯一的 threadEntry()。
也许可以(在实例中)安装“int select”,以便 threadEntry() 中的后续开关/案例可以运行独特的方法或 threadEntry2_x()。
或者 switch/case 可能安装在 threadEntry2() 中。
考虑 startApp 参数可能是一个方法指针。
void Foo_t::startApp(<method pointer>);
可以(更直接地)调用方法指针而不是“固定”名称 threadEntry2()。
以上都是小问题。
Mutex 和在一个实例中运行超过 1 个线程是更大的问题。
我确实在单个类实例中有多个线程“运行”。为此,我在互斥锁或其他一些保护机制下使用了临界区。 std::mutex 很方便,可以与“Posix”线程一起使用,但是,在 Ubuntu 上,我经常使用 Posix Process Semaphore,设置为本地模式(未命名,未共享)。 PPLSem_t 是高效的,适合包装在一个小类中的 4 个单行方法。
pthread_create tho allows only a static function as starting routine, so it can't be instantiated.
实例化包含静态方法的类的实例并不困难。我不确定您在此声明/上下文中的意思。
回顾我在上面详述的方法,您应该可以快速开始在您的类实例中运行 Posix 线程。
请记住检查堆栈使用情况以及您的 ARM 系统上有多少 ram 可用。 Ubuntu 默认堆栈大小为 8 MBytes。也许您的 ARM 提供堆栈大小控制。
关于c++ - Posix 线程类和启动例程 (pthread),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845393/