none
Is there any additional changes do I need to do for Linux platform for thread creation when using for both Windows and Linux platforms using C++? RRS feed

  • Question

  • Hello All,

    I am creating a thread in the library for different platforms using #ifdef and calling it from the main application like as shown below code snippet:

    When I compile the code it is working for windows platform. I also wrote the code to support for Linux platform and that will be useful when we run the code in the Linux platform.

    But I wanted to know, is my syntax of "pthread_create" is correct?
    And also can I pass the same thread function (mythread function I am passing to CreateThread function) as a parameter to "pthread_create" also?
    Is there any additional changes do I need to do to use the thread creation for Linux platform also?

    Library code (mydll):

    MyDLL.h

    #ifdef MYDLL_EXPORTS
    #define THREADDLL_API __declspec(dllexport) 
    #else
    #define THREADDLL_API __declspec(dllimport) 
    #endif

    #include "stdafx.h"
    #include <Windows.h>
    #include <stdio.h>

        class ThreadClass
        {
        public: 
            static THREADDLL_API DWORD common_createthread();

        };


    MyDLL.cpp

    #include "MyDLL.h"

    using namespace std;

    DWORD WINAPI mythread(__in LPVOID lpParameter)
    {
    printf("Thread inside %d \n", GetCurrentThreadId());
    return 0;



        DWORD ThreadClass::common_createthread()
        {
    HANDLE myhandle;
    DWORD mythreadid;

    #ifdef WIN32
    myhandle = CreateThread(0, 0, mythread, 0, 0, &mythreadid);
    #elif
    pthread_t ptid; 
    int err; 
    err = pthread_create(&ptid, NULL, &mythread, NULL); 
    #endif
    return 0;
        }


    Application (mainapplication)

    mainapplicatin.cpp

    #include "stdafx.h"
    #include "MyDLL.h"

    int _tmain(int argc, _TCHAR* argv[])
    {
    ThreadClass threadobj;
    threadobj.common_createthread();
    getchar();
    return 0;
    }

    Regards,

    Wednesday, March 18, 2020 2:24 PM

All replies

  • Hi,

    Thank you for posting here.

    >>But I wanted to know, is my syntax of "pthread_create" is correct?
    And also can I pass the same thread function (mythread function I am passing to CreateThread function) as a parameter to "pthread_create" also?
    Is there any additional changes do I need to do to use the thread creation for Linux platform also?

    "pthread.h" is a header for the Unix/Linux (POSIX) API for threads. The native Windows threading API is exposed via #include <windows.h> and it works slightly differently to Linux's threading.

    "pthread_create" is not in standard C++ and is not supported by VC++.

    Our forum is discussing about the Visual C++ language, compiler, and linker. since your issue contains Linux,pthreads, it is likely that it is beyond our support. so we suggest you could open a new thread in stack overflow with c++, Linux,pthreads tag for better support.

    Best Regards,

    Jeanine Zhang

    Thursday, March 19, 2020 1:36 AM
  • All I can say is that thread system in Linux works differently than in Windows.

    So the same code would not be executed properly until some changes are made to it.

    Also there is be variation in results in both the systems if the code is run in both of them.

    Friday, March 20, 2020 11:49 AM