- 论坛徽章:
- 0
|
- #include <iostream>
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- using namespace std;
- int number = 1;
- void* thread1(void*)
- {
- for(int i = 0; i < 10; i++)
- {
- cout << "this is thread1, number is " << number++;
- cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
- }
- }
- void* thread2(void*)
- {
- for(int i = 0; i < 10; i++)
- {
- cout << "this is thread2, number is " << number++;
- cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
- }
- }
- int main()
- {
- typedef void* (*thre)(void*);
- pthread_t tid[2];
- //thre thre1 = thread1;
-
- pthread_create(&tid[1], NULL, thread1, NULL);
- pthread_create(&tid[2], NULL, thread2, NULL);
-
- cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
- pthread_join(tid[1], NULL);
- pthread_join(tid[2], NULL);
- return 0;
- }
复制代码
上面的程序可以正常编译,但是下面的就不行了
- #include <iostream>
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- using namespace std;
- class Thread
- {
- private:
- int number;
- pthread_t tid[2];
- public:
- Thread()
- {
- number = 1;
- pthread_create(&tid[1], NULL, thread1, NULL);
- pthread_create(&tid[2], NULL, thread2, NULL);
- }
- ~Thread()
- {
- cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
- pthread_join(tid[1], NULL);
- pthread_join(tid[2], NULL);
- }
- void* thread1(void*)
- {
- for(int i = 0; i < 10; i++)
- {
- cout << "this is thread1, number is " << number++;
- cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
- }
- }
- void* thread2(void*)
- {
- for(int i = 0; i < 10; i++)
- {
- cout << "this is thread2, number is " << number++;
- cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
- }
- }
- };
- int main()
- {
- Thread *th = new Thread();
- delete(th);
-
- return 0;
- }
复制代码
出现的错误是
- thread.cpp: In constructor `Thread::Thread()':
- thread.cpp:17: no matches converting function `thread1' to type `
- void*(*)(void*)'
- thread.cpp:29: candidates are: void* Thread::thread1(void*)
- thread.cpp:18: no matches converting function `thread2' to type `
- void*(*)(void*)'
- thread.cpp:38: candidates are: void* Thread::thread2(void*)
复制代码
使用tepdef void*(*thre)(void*); thre ttt1 = thread1;作为参数还是不行
不晓得是甚么原因????
望高人指点 |
|