中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. "); //-->

        博客專欄

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

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

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

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

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

        #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)行編譯的時候,這個家伙: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;
        這個pthread_create()中的第三個參數(shù)是載入一個函數(shù),這個函數(shù)有一個參數(shù)可以傳入,返回一個 通用指針。
        因此,出現(xiàn)上述錯誤的解決方法:
        1)線程函數(shù)定義為void  thread(void* arg),而調(diào)用處寫為: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,搞定!
        注意,這里只能寫gcc,究其原因就是C語言編譯器允許隱含性的將一個通用指針轉(zhuǎn)換為任意類型的指針,而C++不允許的。
        ————————————————

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


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

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


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

        相關(guān)推薦

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

        關(guān)閉