亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 4899 | 回復: 14
打印 上一主題 下一主題

[C++] linux下c++的thread問題 [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2006-11-13 15:29 |只看該作者 |倒序瀏覽

  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. using namespace std;

  6. int number = 1;

  7. void* thread1(void*)
  8. {
  9.   for(int i = 0; i < 10; i++)
  10.   {
  11.     cout << "this is thread1, number is " << number++;
  12.     cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  13.   }
  14. }

  15. void* thread2(void*)
  16. {
  17.   for(int i = 0; i < 10; i++)
  18.   {
  19.     cout << "this is thread2, number is " << number++;
  20.     cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  21.   }
  22. }

  23. int main()
  24. {
  25.   typedef void* (*thre)(void*);
  26.   pthread_t tid[2];
  27.   //thre thre1 = thread1;
  28.   
  29.   pthread_create(&tid[1], NULL, thread1, NULL);
  30.   pthread_create(&tid[2], NULL, thread2, NULL);
  31.   
  32.   cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
  33.   pthread_join(tid[1], NULL);
  34.   pthread_join(tid[2], NULL);

  35.   return 0;
  36. }
復制代碼

上面的程序可以正常編譯,但是下面的就不行了

  1. #include <iostream>
  2. #include <pthread.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. using namespace std;

  6. class Thread
  7. {
  8.   private:
  9.     int number;
  10.     pthread_t tid[2];

  11.   public:
  12.     Thread()
  13.     {
  14.       number = 1;
  15.       pthread_create(&tid[1], NULL, thread1, NULL);
  16.       pthread_create(&tid[2], NULL, thread2, NULL);
  17.     }

  18.     ~Thread()
  19.     {
  20.       cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
  21.       pthread_join(tid[1], NULL);
  22.       pthread_join(tid[2], NULL);
  23.     }

  24.     void* thread1(void*)
  25.     {
  26.       for(int i = 0; i < 10; i++)
  27.       {
  28.         cout << "this is thread1, number is " << number++;
  29.         cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  30.       }
  31.     }

  32.     void* thread2(void*)
  33.     {
  34.       for(int i = 0; i < 10; i++)
  35.       {
  36.         cout << "this is thread2, number is " << number++;
  37.         cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
  38.       }
  39.     }
  40. };

  41. int main()
  42. {
  43.   Thread *th = new Thread();
  44.   delete(th);
  45.   
  46.   return 0;
  47. }
復制代碼

出現(xiàn)的錯誤是


  1. thread.cpp: In constructor `Thread::Thread()':
  2. thread.cpp:17: no matches converting function `thread1' to type `
  3.    void*(*)(void*)'
  4. thread.cpp:29: candidates are: void* Thread::thread1(void*)
  5. thread.cpp:18: no matches converting function `thread2' to type `
  6.    void*(*)(void*)'
  7. thread.cpp:38: candidates are: void* Thread::thread2(void*)
復制代碼

使用tepdef void*(*thre)(void*); thre ttt1 = thread1;作為參數(shù)還是不行
不曉得是甚么原因????
望高人指點

論壇徽章:
0
2 [報告]
發(fā)表于 2006-11-13 16:11 |只看該作者

回復 1樓 clyman 的帖子

似乎在那里看到,要把線程函數(shù)體定義在類外部,或者前加“static”

論壇徽章:
0
3 [報告]
發(fā)表于 2006-11-13 16:12 |只看該作者
但是他報的是類型轉換....

論壇徽章:
0
4 [報告]
發(fā)表于 2006-11-13 17:07 |只看該作者
原帖由 king_wuhan 于 2006-11-13 16:11 發(fā)表
似乎在那里看到,要把線程函數(shù)體定義在類外部,或者前加“static”




我試了一下  好象不太行啊

論壇徽章:
0
5 [報告]
發(fā)表于 2006-11-14 10:18 |只看該作者
線程函數(shù)訪問私有變量,可以分兩步實現(xiàn)
(1)函數(shù)聲明為靜態(tài)函數(shù);
(2)在調(diào)用函數(shù)的時候將實例的指針this以void *的形式傳遞進去;

#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, this);
      pthread_create(&tid[2], NULL, thread2, this);
    }

    ~Thread()
    {
      cout << "tid1 is " << tid[1] << " tid2 is " << tid[2] << endl;
      pthread_join(tid[1], NULL);
      pthread_join(tid[2], NULL);
    }

   static void* thread1(void* arg)
    {
      Thread* nthis=(Thread *)arg;
      for(int i = 0; i < 10; i++)
      {
        cout << "this is thread1, number is " << number++;
        cout << " pid is " << getpid() << " id is " << pthread_self() << endl;
      }
    }

   static void* thread2(void* arg)
    {
      Thread* nthis=(Thread *)arg;
      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;
}

論壇徽章:
0
6 [報告]
發(fā)表于 2006-11-14 10:33 |只看該作者
原帖由 billzhou 于 2006-11-14 10:18 發(fā)表
線程函數(shù)訪問私有變量,可以分兩步實現(xiàn)
(1)函數(shù)聲明為靜態(tài)函數(shù);
(2)在調(diào)用函數(shù)的時候將實例的指針this以void *的形式傳遞進去;

#include <iostream>
#include <pthread.h>
#include < ...



編譯通不過啊,呵呵

論壇徽章:
0
7 [報告]
發(fā)表于 2006-11-14 11:15 |只看該作者
原帖由 billzhou 于 2006-11-14 10:18 發(fā)表
線程函數(shù)訪問私有變量,可以分兩步實現(xiàn)
(1)函數(shù)聲明為靜態(tài)函數(shù);
(2)在調(diào)用函數(shù)的時候將實例的指針this以void *的形式傳遞進去;

#include <iostream>
#include <pthread.h>
#include < ...



忘了  函數(shù)體內(nèi)改一下    nthis.number++就可以了

論壇徽章:
0
8 [報告]
發(fā)表于 2006-11-14 11:21 |只看該作者
把線程函數(shù)寫成全局函數(shù)

論壇徽章:
0
9 [報告]
發(fā)表于 2006-11-14 19:17 |只看該作者
參見類成員函數(shù)的帖子,設置為static是對的

論壇徽章:
39
2017金雞報曉
日期:2017-02-08 10:39:4219周年集字徽章-周
日期:2023-04-15 12:02:2715-16賽季CBA聯(lián)賽之深圳
日期:2023-02-16 14:39:0220周年集字徽章-年
日期:2022-08-31 14:25:28黑曼巴
日期:2022-08-17 18:57:0919周年集字徽章-年
日期:2022-04-25 13:02:5920周年集字徽章-20	
日期:2022-03-29 11:10:4620周年集字徽章-年
日期:2022-03-14 22:35:1820周年集字徽章-周	
日期:2022-03-09 12:51:3220周年集字徽章-年
日期:2022-02-10 13:13:4420周年集字徽章-周	
日期:2022-02-03 12:09:4420周年集字徽章-20	
日期:2022-01-25 20:14:27
10 [報告]
發(fā)表于 2006-11-14 19:35 |只看該作者
樓上兩個對。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP