<Windows.h> c++ 线程和继承
<Windows.h> c++ threads and inheritance
大家好,我正在研究一个线程类和一个CountingThread类,从线程类继承,包括使用库的同步计数器。但是在创建这个CountingThread类时,我遇到了一个"不允许不完整类型"的问题,所以如果你给我一些建议,如果我在一个糟糕的结构中形成这个线程抽象类,或者说我做错了什么,我会很高兴。(仅供参考,我必须保持类和方法,因为它是一个分配)
#ifndef _THREAD_H_
#define _THREAD_H_
#include <Windows.h>
#include <iosfwd>
class Thread{
private:
HANDLE hThread;
int idThread;
public:
Thread(LPTHREAD_START_ROUTINE fnct){ // here I'm trying to get a function and create thread with it
hThread = CreateThread(NULL, 0,fnct,NULL,0,(LPDWORD)&idThread);
}
virtual void main()=0;
void suspend(){
SuspendThread(hThread);
}
void resume(){
ResumeThread(hThread);
}
void terminate(){
TerminateThread(hThread,0);
}
static void sleep(int sec){
Sleep(sec*1000);
}
};
#endif
CountingThread.h
#ifndef _COUNTINGTHREAD_H_
#define _COUNTINGTHREAD_H_
#include "SynchronizedCounter.h"
#include "Thread.h"
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread(counter.increment()){ // here I'm having the error "incomplete type on counter"
} // I want to create thread with the counter.increment function
};
#endif
SynchronizedCounter.h
#ifndef SYNCHRONIZEDCOUNTER_H_
#define SYNCHRONIZEDCOUNTER_H_
#include "Mutex.h"
#include <iosfwd>
class SynchronizedCounter{
private:
int count;
public:
SynchronizedCounter();
SynchronizedCounter(int);
void increment();
int value();
friend std::ostream &operator <<(std::ostream& output, const SynchronizedCounter& counter)
{
output << counter.count << endl;
return output;
}
};
#endif
和synchronizedCounter::increment
void SynchronizedCounter::increment(){
Mutex mut;
mut.lock;
count++;
mut.unlock;
}
似乎是语法错误。你应该在这里定义一个参数:
所以应该是:
class CountingThread :public Thread{
private:
SynchronizedCounter counter;
public:
CountingThread()
{
counter.increment())
//... etc
} // I want to create thread with the counter.increment function
//...
};
无论如何counter.increment()返回void,你不能把它作为参数传递。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- 继承、后台线程和 RAII
- 停止线程的方法是什么(当我直接从Qthread继承时)
- Qt Worker 线程 - 我必须为每个特定任务继承,还是可以使用回调
- 从父线程继承全局值
- 如何为QWidget继承的类创建线程
- <Windows.h> c++ 线程和继承
- 在继承的类中启动线程
- 是否有任何cpp函数或对象(不包括从c继承的)不是线程安全的,即使每个线程对自己的数据进行操作
- c++ 11的thread_local变量能从父线程继承它的初始值吗?