Comparison of Windows and Linux synchronization APIs

Source: Internet
Author: User

Initialize critical section

(Win)

InitializeCriticalSection (rtl_critical_section &rtl_critial_section)

(Linux)

Pthread_mutexattr_init (& (Mutex)->attr);

Pthread_mutexattr_settype (& (Mutex)->attr, pthread_mutex_recursive);

Pthread_mutex_init (& (Mutex)->MTX, & (Mutex)->attr);

Delete a critical section

(Win)

DeleteCriticalSection (Rtl_critical_section &)

(Linux)

Pthread_mutex_destroy (pthread_mutex_t &mutex)

Enter the critical section

(Win)

EnterCriticalSection (rtl_critical_section &rtl_critical_section)

(Linux)

Pthread_mutex_lock (pthread_mutex_t &mutex)

Try to enter the critical section

(Win)

TryEnterCriticalSection (rtl_critical_section &rtl_critical_section)

(Linux)

Pthread_mutex_trylock (pthread_mutex_t &mutex)

Leave the critical section

(Win)

LeaveCriticalSection (rtl_critical_section &rtl_critical_section)

(Linux)

Pthread_mutex_unlock (pthread_mutex_t &mutex)

Compares the target operand (the number of memory pointed to by the 1th parameter) with a value (the 3rd parameter) and, if equal, with another value (the 2nd parameter) and the target operand (the number of memory pointed to by the 1th parameter); InterlockedExchange is not a comparison of direct exchanges. The entire operation is locked in memory, other processors do not access the memory at the same time, thus enabling thread mutexes in multiprocessor environments

(Win)

InterlockedCompareExchange (Destination, NewValue, Oper)

(Linux)

__sync_val_compare_and_swap (Destination, Oper, newvalue)

V The value of the atom adds the size of P

(Win)

InterlockedExchangeAdd (V, P)

(Linux)

__sync_fetch_and_add (V, P)

Atoms Add a

(Win)

InterlockedIncrement (T)

(Linux)

__sync_fetch_and_add (T, 1)

Atoms reduce One

(Win)

InterlockedDecrement (T)

(Linux)

__sync_fetch_and_sub (T, 1)

Gets the current thread ID

(Win)

GetCurrentThreadID ()

(Linux)

Syscall (Sys_gettid)

If you specify a value other than 0, the function waits until the Hhandle tag object is triggered, or the time is up. If Dwmilliseconds is 0, the object is not signaled, and the function does not enter a wait state, and it always returns immediately. If Dwmilliseconds is infinite, the function will not return until the object is signaled. The corresponding Linux implementations use condition variables

(Win)

WaitForSingleObject (Event,infinite)

(Linux)

Pthread_mutex_lock (&M_TX);
Pthread_cond_wait (&event, &M_TX);
Pthread_mutex_unlock (&M_TX);

Exit thread (exit parameter 0)

(Win)

ExitThread (0)

(Linux)

Pthread_exit (0)

Set thread priority, Pthread_setschedparam is often used in multithreaded development, and is used primarily to set thread invocation policies and priorities

(Win)

SetThreadPriority (Handle,nprioroty)

(Linux)

Sched_param sp = {npriority};
if (0 = = Pthread_setschedparam (M_pid, SCHED_RR, &SP))
{
return true;
}
return false;

Get priority level

(Win)

GetThreadPriority (Handle m_hthread)

(Linux)

int policy;
Sched_param sp;
Pthread_getschedparam (M_pid, &policy, &SP))
sp.sched_priority;

Initialize Mutex amount

(Linux)

Pthread_mutex_init (pthread_mutex_t &mutex), 0)

Initializing condition variables

Pthread_cond_init (&cond,0)

Delete Mutex amount

Pthread_mutex_destroy (pthread_mutex_t &mutex))

Delete a condition variable

(Linux)

Pthread_cond_destroy (pthread_cond_t &cond)

Signaling to a condition variable

(Linux)

Pthread_cond_signal (pthread_cond_t &cond)

Suspend wait end (infinite wait) true to allow access to warning state during blocking (Windows only)

(Win)

WaitForSingleObject Ex (handle, Infinite,true)

(Linux)

Pthread_join (pthread_t thid, void * * ret_val) Common pthread_join (pid,0)

Comparison of Windows and Linux synchronization APIs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.