Pelles C forum

C language => Beginner questions => Topic started by: siwenjo on January 02, 2014, 09:51:50 PM

Title: What is the best cross-platform way to multi-thread using PellesC?
Post by: siwenjo on January 02, 2014, 09:51:50 PM
My understanding is that threads are not part of the ansi spec and that an external library is required. pThreads is the posix designed for Unix but ported to Windows; is this the best way to go, and if so is there a link to setting up pThreads for PellesC 64bit?
Title: Re: What is the best cross-platform way to multi-thread using PellesC?
Post by: DMac on January 03, 2014, 12:54:48 AM
I found this code somewhere several years ago:


#if defined(LINUX)

//Headers
#include <pthread.h>

//Data types
#define MUTEX pthread_mutex_t

#ifndef EBUSY
#define EBUSY 16 // resource busy
#endif

//Macroes
#define MUTEX_INIT(mutex) (0 == pthread_mutex_init (&(mutex), NULL))
    #define MUTEX_DESTROY(mutex) (0 == pthread_mutex_destroy(&(mutex)))
#define MUTEX_LOCK(mutex) (0 == pthread_mutex_lock (&(mutex)))
#define MUTEX_UNLOCK(mutex) (0 == pthread_mutex_unlock (&(mutex)))
#define MUTEX_TRYLOCK(mutex) (EBUSY == pthread_mutex_trylock (&(mutex)))

#elif defined(WINDOWS)

//Headers
#include <windows.h>
#include <process.h>

//Data types
#define MUTEX HANDLE

//Macroes
#define MUTEX_INIT(mutex) (NULL != ((mutex) = CreateMutex (0, FALSE, 0)))
    #define MUTEX_DESTROY(mutex) (0 != CloseHandle((mutex)))
#define MUTEX_LOCK(mutex) (WAIT_FAILED == WaitForSingleObject ((mutex), INFINITE))
#define MUTEX_UNLOCK(mutex) (0 == ReleaseMutex ((mutex)))
#define MUTEX_TRYLOCK(mutex) (WAIT_TIMEOUT == WaitForSingleObject((mutex), IGNORE))

#else

//Data types
#define MUTEX Please_define_either_WINDOWS_or_LINUX_in_sourcecode

#endif


These macros provide a simple way to write cross platform code that runs in a multithreaded environment.

Title: Re: What is the best cross-platform way to multi-thread using PellesC?
Post by: siwenjo on January 03, 2014, 02:06:02 AM
Well I have just been doing some reading and I understand that the c11 specification, which I believe PellesC implements contains both threading and atomic variables. Which is excellently what I was after. So does anyone know a link to any tutorials on using these features?