Skip to content

Testcases about POSIX Threads --Unix-based & Linux Systems

Notifications You must be signed in to change notification settings

Electrostat-Lab/CThreads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CThreads

Let's dive into CThreads Interface aka POSIX Threads which isn't shipped natively with C, it's based on Unix :

https://www.ibm.com/docs/en/zos/2.4.0?topic=files-pthreadh-thread-interfaces

Resources for POSIX Threads of Unix :

https://www.ibm.com/docs/en/i/7.1?topic=ssw_ibm_i_71/apis/concept8.htm
https://www.geeksforgeeks.org/thread-functions-in-c-c/

Mutex -- Mutually Exclusive -- Synchronized -- Object :

https://www.ibm.com/docs/en/zos/2.3.0?topic=functions-pthread-mutex-init-initialize-mutex-object

struct timespec --used by the Mutex System of pthread.h in C method :

#include <bits/types/struct_timespec.h>
/* Wait until lock becomes available, or specified time passes. */
extern int pthread_mutex_timedlock (pthread_mutex_t *__restrict __mutex,
				    const struct timespec *__restrict__abstime);

struct timespec -- source from GNU :

/* NB: Include guard matches what <linux/time.h> uses.  */
#ifndef _STRUCT_TIMESPEC
#define _STRUCT_TIMESPEC 1

#include <bits/types.h>
#include <bits/endian.h>

/* POSIX.1b structure for a time value.  This is like a `struct timeval' but
   has nanoseconds instead of microseconds.  */
struct timespec
{
  __time_t tv_sec;		/* Seconds.  */
#if __WORDSIZE == 64 \
  || (defined __SYSCALL_WORDSIZE && __SYSCALL_WORDSIZE == 64) \
  || __TIMESIZE == 32
  __syscall_slong_t tv_nsec;	/* Nanoseconds.  */
#else
# if __BYTE_ORDER == __BIG_ENDIAN
  int: 32;           /* Padding.  */
  long int tv_nsec;  /* Nanoseconds.  */
# else
  long int tv_nsec;  /* Nanoseconds.  */
  int: 32;           /* Padding.  */
# endif
#endif
};

#endif

https://en.cppreference.com/w/c/chrono/timespec

To Build, use shell command/terminal :

┌─[twisted@parrot]─[~/GradleProjects/CThreads/build]
└──╼ $./compile.sh 
Compiling the project
Successfully Compiled

To run :

┌─[twisted@parrot]─[~/GradleProjects/CThreads/output]
└──╼ $./ThreadsTest.exec
Main Thread joined
Async 2 joined
139843706689280 Aysnc 2 Terminated
Thread 1
Async 1 joined
139843715081984 Aysnc 1 Terminated
139843698296576 (Thread 1) has been detached & terminated
Thread 2
139843698296576 (Thread 2) has been detached & terminated
mutex_initialized & property settled to NORMAL single lock mutually events
mutex object obtained by Event 1 -- Event 1 execution starts
mutex_locked to this Event 1, concurrent threads will wait until unlock occurs
mutex object obtained by Event 2 -- Event 2 execution starts
mutex_locked to this Event 2, concurrent threads will wait until unlock occurs

Tips : You can open the code on vscode, use c++ code completion, syntax highlighting.