I am creating a thread in the library for different platforms using #ifdef and calling it from the main application like as shown in the below code snippet:
When I compile the code it is working for windows platform. I also wrote the code to support for Linux platform as 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;
}
Aucun commentaire:
Enregistrer un commentaire