您當前的位置:首頁 > 舞蹈

C++11 鎖機制簡單對比 - atomic, mutex, spinlock

作者:由 xlindo 發表于 舞蹈時間:2020-05-26

0 結論

對單個變數多執行緒下的簡單操作,速度由快到慢(時間由短到長)分別為

原子操作(無鎖) atomic,互斥鎖 mutex ~ lock_guard,自旋鎖 spinlock

自旋鎖 spinlock 得自己用 CAS 或者說 atomic 實現

1 程式碼

#include

#include

#include

#include

#include

#include

using

namespace

std

class

SpinLock

{

public

SpinLock

()

flag_

false

{}

void

lock

()

{

bool

expected

=

false

while

flag_

compare_exchange_weak

expected

true

))

{

expected

=

false

}

}

void

unlock

()

{

flag_

store

false

);

}

private

atomic

<

bool

>

flag_

};

SpinLock

SL

long

total

=

0

atomic

<

long

>

at_total

{

0

};

mutex

m

void

in_thread

()

{

for

auto

i

=

0

i

<

10000000

++

i

{

++

total

}

}

void

in_mutex

()

{

for

auto

i

=

0

i

<

10000000

++

i

{

m

lock

();

++

total

m

unlock

();

}

}

void

in_lock_guard

()

{

for

auto

i

=

0

i

<

10000000

++

i

{

lock_guard

<

mutex

>

lock

m

);

++

total

}

}

void

in_atomic

()

{

for

auto

i

=

0

i

<

10000000

++

i

{

++

at_total

}

}

void

in_spinlock

()

{

for

auto

i

=

0

i

<

10000000

++

i

{

SL

lock

();

++

total

SL

unlock

();

}

}

int

main

()

{

int

thread_num

=

10

vector

<

thread

>

v_thread

// in multi-thread

clock_t

t_start

=

clock

();

total

=

0

for

auto

i

=

0

i

<

thread_num

++

i

{

v_thread

emplace_back

thread

in_thread

));

}

for_each

v_thread

begin

(),

v_thread

end

(),

[](

std

::

thread

&

t

{

if

t

joinable

())

t

join

();

});

clock_t

t_end

=

clock

();

cout

<<

“Result of in multi-thread, total = ”

<<

total

<<

endl

cout

<<

“Time of in multi-thread: ”

<<

t_end

-

t_start

<<

endl

// in atomic

t_start

=

clock

();

total

=

0

for

auto

i

=

0

i

<

thread_num

++

i

{

v_thread

emplace_back

thread

in_atomic

));

}

for_each

v_thread

begin

(),

v_thread

end

(),

[](

std

::

thread

&

t

{

if

t

joinable

())

t

join

();

});

t_end

=

clock

();

cout

<<

“Result of in_atomic, total = ”

<<

at_total

<<

endl

cout

<<

“Time of in_atomic: ”

<<

t_end

-

t_start

<<

endl

// in mutex

t_start

=

clock

();

total

=

0

for

auto

i

=

0

i

<

thread_num

++

i

{

v_thread

emplace_back

thread

in_mutex

));

}

for_each

v_thread

begin

(),

v_thread

end

(),

[](

std

::

thread

&

t

{

if

t

joinable

())

t

join

();

});

t_end

=

clock

();

cout

<<

“Result of in_mutex, total = ”

<<

total

<<

endl

cout

<<

“Time of in_mutex: ”

<<

t_end

-

t_start

<<

endl

// in lock_guard

t_start

=

clock

();

total

=

0

for

auto

i

=

0

i

<

thread_num

++

i

{

v_thread

emplace_back

thread

in_lock_guard

));

}

for_each

v_thread

begin

(),

v_thread

end

(),

[](

std

::

thread

&

t

{

if

t

joinable

())

t

join

();

});

t_end

=

clock

();

cout

<<

“Result of in_lock_guard, total = ”

<<

total

<<

endl

cout

<<

“Time of in_lock_guard: ”

<<

t_end

-

t_start

<<

endl

// in spinlock

t_start

=

clock

();

total

=

0

for

auto

i

=

0

i

<

thread_num

++

i

{

v_thread

emplace_back

thread

in_spinlock

));

}

for_each

v_thread

begin

(),

v_thread

end

(),

[](

std

::

thread

&

t

{

if

t

joinable

())

t

join

();

});

t_end

=

clock

();

cout

<<

“Result of in_spinlock, total = ”

<<

total

<<

endl

cout

<<

“Time of in_spinlock: ”

<<

t_end

-

t_start

<<

endl

return

0

}

2 輸出

Result of in multi-thread, total = 15140352

Time of in multi-thread: 3078125

Result of in_atomic, total = 100000000

Time of in_atomic: 18187500

Result of in_mutex, total = 100000000

Time of in_mutex: 78203125

Result of in_lock_guard, total = 100000000

Time of in_lock_guard: 89984375

Result of in_spinlock, total = 100000000

Time of in_spinlock: 242750000

參考

https://

blog。csdn。net/natahew/a

rticle/details/48808499

標簽: Thread  total  ++  end  lock