日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          "); //-->

          博客專欄

          EEPW首頁(yè) > 博客 > c++創(chuàng)建線程的常見(jiàn)問(wèn)題:error: invalid conversion from 'void*' to 'void* (*)(void*)'

          c++創(chuàng)建線程的常見(jiàn)問(wèn)題:error: invalid conversion from 'void*' to 'void* (*)(void*)'

          發(fā)布人:電子禪石 時(shí)間:2019-12-19 來(lái)源:工程師 發(fā)布文章

          重點(diǎn):注意,這里只能寫(xiě)gcc,究其原因就是C語(yǔ)言編譯器允許隱含性的將一個(gè)通用指針轉(zhuǎn)換為任意類型的指針,而C++不允許的。

          本人近期在做按tcp流發(fā)送數(shù)據(jù)包的學(xué)習(xí),一來(lái)二去接觸到了多線程。我百度照此寫(xiě)了個(gè)多線程代碼,如下:

          #include <pthread.h>
          #include <stdio.h>
          #include <stdlib.h>
          void thread(void *arg);
          void thread(void *arg)
          {
              int i;
              for(i = 0; i < 3; ++i){
                  printf("This is a pthead.\n");
              }
          }
          int main(int argc, char* argv[])
          {
              pthread_t id;
              int i, ret;
              ret = pthread_create(&id, NULL, (void*)&thread, NULL);
              if(ret){
                  printf("create thread error: ");
                  exit(1);
              }
              for(i = 0; i < 3; ++i){
                  printf("This is main proccess");
              }
              pthread_join(id, NULL);
              return 0;
          }
          但是,他爹,我用g++ main.cpp -o -lpthread debug進(jìn)行編譯的時(shí)候,這個(gè)家伙:error: invalid conversion from 'void*' to 'void* (*)(void*)'不厭其煩地出現(xiàn)在了我的xshell中,弄得我是苦不堪言。
          于是,我查看了Posix中建立線程函數(shù)的定義:extern int pthread_create (pthread_t *__restrict __threadp,  __const pthread_attr_t *__restrict __attr, void(*__start_routine) (void *), void *__restrict __arg) __THROW;
          這個(gè)pthread_create()中的第三個(gè)參數(shù)是載入一個(gè)函數(shù),這個(gè)函數(shù)有一個(gè)參數(shù)可以傳入,返回一個(gè) 通用指針。
          因此,出現(xiàn)上述錯(cuò)誤的解決方法:
          1)線程函數(shù)定義為void  thread(void* arg),而調(diào)用處寫(xiě)為:int ret = pthread_create(&id, NULL, (viod*)&thread, NULL);
          2)線程函數(shù)定義為void * thread(void* arg),調(diào)用處為:int ret = pthead_create(&id, NULL, thread, NULL)。
          然后進(jìn)行編譯: gcc main.c -o -lpthread debug,搞定!
          注意,這里只能寫(xiě)gcc,究其原因就是C語(yǔ)言編譯器允許隱含性的將一個(gè)通用指針轉(zhuǎn)換為任意類型的指針,而C++不允許的。
          ————————————————

          原文鏈接:https://blog.csdn.net/Alpelious/article/details/53486547


          *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。

          電抗器相關(guān)文章:電抗器原理


          關(guān)鍵詞: c++

          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉