#include <iostream>
#include <Windows.h>
#include <process.h>
#include <time.h>
#include <conio.h>
using namespace std;
class Klasa
{
public:
void petla(void* Args)
{
time_t tWait = clock() + 4 * CLOCKS_PER_SEC;
for(;;)
{
cout << "Test dzialania watkow" << endl;
Sleep(1000);
}
_endthread();
}
};
void main()
{
Klasa* pKlasa = new Klasa;
time_t tCzas = clock() + 10 * CLOCKS_PER_SEC;
_beginthread(pKlasa->petla, 0, NULL);
while(tCzas>=clock())
{
cout << " Dziala" << endl;
Sleep(500);
}
getch();
}
Error 1 error C3867: 'Klasa::petla': function call missing argument list; use '&Klasa::petla' to create a pointer to member c:\users\bartek\documents\visual studio 2012\projects\wątki\wątki\source.cpp 26 1 Wątki
错误1错误C3867:“Klasa::petla”:函数调用缺失参数列表;使用“&Klasa::petla”来创建一个指向成员c的指针:\ \用户\bartek\ \ \ \visual studio 2012\ tki\w tki\source。cpp 26 1 Wątki
This is an error and I don't know What shoud I do cause I can't put ()
in this beginthread(pKlasa->petla, 0, NULL);
Guys please help me :C
这是一个错误,我不知道我要做什么,因为我不能把()在这个开始线程(pKlasa->petla, 0, NULL);请帮帮我:C。
4 个解决方案
#1
4
Klasa::petla
needs to be declared static if you want it to be the entry point of a thread.
:如果你想让petla成为线程的入口点,它需要被声明为静态的。
The typical idiom for starting a thread within an object without leaking access to anything important or thread-dangerous looks something like this:
用于在对象中启动线程,而不泄漏任何重要或线程危险的线程的典型用法如下:
#include <iostream>
#include <Windows.h>
#include <process.h>
#include <time.h>
class Klasa
{
public:
void Start();
private:
static void ThreadEntry(void *p);
void ThreadBody();
};
void Klasa::Start()
{
_beginthread(Klasa::ThreadEntry, 0, this);
}
void Klasa::ThreadEntry(void *p)
{
((Klasa *) p)->ThreadBody();
_endthread();
return;
}
void Klasa::ThreadBody()
{
// do threaded action here
time_t tWait = clock() + 4 * CLOCKS_PER_SEC;
for(;;)
{
cout << "Test dzialania watkow" << endl;
Sleep(1000);
}
}
void main()
{
Klasa k;
k.Start();
time_t tCzas = clock() + 10 * CLOCKS_PER_SEC;
while(tCzas>=clock())
{
cout << " Dziala" << endl;
Sleep(500);
}
char c;
c << std::cin; // stick to either cin/cout or C-style IO, not both
}
At least, that's how I tend to do it using pthreads. I'd imagine it's basically the same with windows threads.
至少,这是我使用pthreads的方式。我想它基本上和windows线程是一样的。
Also, please try to avoid using Hungarian Notation. It's personal preference, but there are a lot of good arguments for not using it (like the fact that C++ is strongly typed and the type of every variable or function is apparent from its definition).
另外,请尽量避免使用匈牙利符号。这是个人偏好,但是有很多很好的理由不使用它(比如,c++是强类型的,而且每个变量或函数的类型从定义中都很明显)。
#2
3
From the documentation:
从文档:
uintptr_t _beginthread(
void( *start_address )( void * ),
unsigned stack_size,
void *arglist
);
So that function takes a pointer to function, and not a pointer-to-member. You can't convert between the two.
所以这个函数需要一个指针来函数,而不是一个pointerto -member。你不能在两者之间转换。
Either use a static
member function (which is tied to the class instead of an instance) or create a non-member function.
要么使用静态成员函数(它与类绑定而不是实例),要么创建一个非成员函数。
#3
2
You must pass an address of function.
你必须通过一个功能地址。
You cannot write though
不过你不能写
_beginthread(&(pKlasa->petla), 0, NULL);
because _beginthread
wants pointer to a function, not a pointer to an object's method. Define a plain function with the required signature:
因为_beginthread需要指向函数的指针,而不是指向对象方法的指针。定义一个具有所需签名的简单函数:
void threadfunc(void *data) {
reinterpret_cast<Klasa*>(data)->petla();
}
and then begin a new thread with
然后开始一个新的线程。
_beginthread(threadfunc, 0, pKlasa);
#4
0
std::thread(&Klasa::petla, pKlasa);
if you don't have a C++11 library you can do the same with Boost.
如果你没有一个c++ 11库,你可以用Boost来做同样的事情。
#1
4
Klasa::petla
needs to be declared static if you want it to be the entry point of a thread.
:如果你想让petla成为线程的入口点,它需要被声明为静态的。
The typical idiom for starting a thread within an object without leaking access to anything important or thread-dangerous looks something like this:
用于在对象中启动线程,而不泄漏任何重要或线程危险的线程的典型用法如下:
#include <iostream>
#include <Windows.h>
#include <process.h>
#include <time.h>
class Klasa
{
public:
void Start();
private:
static void ThreadEntry(void *p);
void ThreadBody();
};
void Klasa::Start()
{
_beginthread(Klasa::ThreadEntry, 0, this);
}
void Klasa::ThreadEntry(void *p)
{
((Klasa *) p)->ThreadBody();
_endthread();
return;
}
void Klasa::ThreadBody()
{
// do threaded action here
time_t tWait = clock() + 4 * CLOCKS_PER_SEC;
for(;;)
{
cout << "Test dzialania watkow" << endl;
Sleep(1000);
}
}
void main()
{
Klasa k;
k.Start();
time_t tCzas = clock() + 10 * CLOCKS_PER_SEC;
while(tCzas>=clock())
{
cout << " Dziala" << endl;
Sleep(500);
}
char c;
c << std::cin; // stick to either cin/cout or C-style IO, not both
}
At least, that's how I tend to do it using pthreads. I'd imagine it's basically the same with windows threads.
至少,这是我使用pthreads的方式。我想它基本上和windows线程是一样的。
Also, please try to avoid using Hungarian Notation. It's personal preference, but there are a lot of good arguments for not using it (like the fact that C++ is strongly typed and the type of every variable or function is apparent from its definition).
另外,请尽量避免使用匈牙利符号。这是个人偏好,但是有很多很好的理由不使用它(比如,c++是强类型的,而且每个变量或函数的类型从定义中都很明显)。
#2
3
From the documentation:
从文档:
uintptr_t _beginthread(
void( *start_address )( void * ),
unsigned stack_size,
void *arglist
);
So that function takes a pointer to function, and not a pointer-to-member. You can't convert between the two.
所以这个函数需要一个指针来函数,而不是一个pointerto -member。你不能在两者之间转换。
Either use a static
member function (which is tied to the class instead of an instance) or create a non-member function.
要么使用静态成员函数(它与类绑定而不是实例),要么创建一个非成员函数。
#3
2
You must pass an address of function.
你必须通过一个功能地址。
You cannot write though
不过你不能写
_beginthread(&(pKlasa->petla), 0, NULL);
because _beginthread
wants pointer to a function, not a pointer to an object's method. Define a plain function with the required signature:
因为_beginthread需要指向函数的指针,而不是指向对象方法的指针。定义一个具有所需签名的简单函数:
void threadfunc(void *data) {
reinterpret_cast<Klasa*>(data)->petla();
}
and then begin a new thread with
然后开始一个新的线程。
_beginthread(threadfunc, 0, pKlasa);
#4
0
std::thread(&Klasa::petla, pKlasa);
if you don't have a C++11 library you can do the same with Boost.
如果你没有一个c++ 11库,你可以用Boost来做同样的事情。